Mac Java help...

AJ Muni

macrumors 65816
Original poster
Aug 4, 2005
1,145
17
Miami
well guys pretty much I need to turn in an assignment next week that consists of a user inputting a number, and I spell it out for them. for example:

Please enter a number: 452 (say user inputs 452)

You entered: Four hundred fifty two.

my teacher explained to break it down, for example there could be 9 hundreds (one hundred, two hundred, three hundred, etc upto nine hundred)

same thing goes for tens, and ones.

This is what I got in my code so far, but I know i'm doing something wrong.
Code:
int hundreds;
int tens;
int ones;
 
 
scanner input = new scanner (system.in);
 
s.o.p("please enter a number:");
hundreds = input.nextInt();
 
if (1 == hundreds)
  {
     system.out.println("One-Hundred);
 }
 
else if (2 ==hundreds)
 
  {
     s.o.p.("Two-hundred);
 }

how can i figure out how to to configure whats in the "tens" position. for example, in the number above, the "5" position.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
You'll obviously need some constants defined. There are efficient ways and inefficient ways to define them.

What constructs (Java data types) do you think you'll need to complete this assignment?

What other code do you have so far? The above looks like brainstorming code.

Have you written out, in plain text, the steps you need to take for doing the analysis? This assignment is more of a "how do I solve the problem?" assignment than a "what is the syntax I need to code?" assignment.

What observations can you make about numbers and how you say them?

For instance, if a number is only two digits long, you will never need: (multiple choice)

A. the constant "one"
B. the constant "hundred"
C. the constant "thirteen"
D. the constant "fifty"

(the above, I'm hoping, you will find quite obvious, but that's the direction you need to be thinking)

Todd
 
Comment

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Here's another BIG hint.

Say these numbers outloud, and recognize what they have in common, and what is different.

1
1,001
1,001,001

Then, this number

452,452

These should give you a big clue about how to structure your code.

Todd
 
Comment

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

I think modulus operator, %, will be very useful in this assignment so you might want to read up on it. For example look at this:-

Code:
theNumber = 3393 ;
thousands = theNumber / 1000 ;
theNumber = theNumber % 1000 ;
The above code would results with thousands=3 and theNumber=393. You can repeat the process to get the hundreds, tens and ones.

b e n
 
Comment

AJ Muni

macrumors 65816
Original poster
Aug 4, 2005
1,145
17
Miami
ok guys i got this so far, but I have no idea how to incorporate the tens and ones.

Code:
import java.util.Scanner;     
    public class numtoword
   {
   
       public static void main(String[]args)
      {                     
       
       int hundreds;
       int tens;
       int ones;
       
       Scanner scan = new Scanner(System.in);
       
       System.out.println("Enter number ");
       int x = scan.nextInt();

       hundreds = x / 100;
       tens = (x % 100) / 10;
       ones = (x % 100)%10;

        if (x==0)
        {
          System.out.println("Zero");
         }
          else if  (x>99 && x<200)
          
          { System.out.println("One Hundred");}

           else if (x>=200 && x<300)
           {System.out.println("Two hundred");}
           
           else if (x>=300 && x<400)
           {System.out.println("Three hundred");}
        
           else if (x>=400 && x<500)
           {System.out.println("Four hundred");}
          
           else if (x>=500 && x<600)
           {System.out.println("Five hundred");}
           
            else if (x>=600 && x<700)
           {System.out.println("Six hundred");}
           
            else if (x>=700 && x<800)
           {System.out.println("Seven hundred");}
           
            else if (x>=800 && x<900)
           {System.out.println("Eight hundred");}
           
            else if (x>=900 && x<1000)
           {System.out.println("Nine hundred");}
        
        
      }
   }
like if the user enters : 587 , i got the "FIVE HUNDRED" figured out, but how would I incorporate the 8 and the 7? if anyone can please help i'll appreciate it. The teacher postponed the assignemnt until tommorow because we are all having problems with it. Someone mentioned to use switches, none of us know how to do it.
 
Comment

SilentPanda

Moderator emeritus
Oct 8, 2002
9,993
30
The Bamboo Forest
I'm assuming your number range is 0 -> 999.

You have the number in x. You then need to find out how many hundreds, how many tens, and how many ones. It looks like you've done that.

For your hundreds you will do a few things. First check to make sure it's greater than 0. If it's not then you can skip your hundreds logic. If it is greater than 0 you should be able to print out: "One", "Two", "Three", etc. Now since it is in the hundreds position you can follow it with a standard printing of "hundred". Do not combine them into printing "One Hundred", "Two Hundred". I'll explain why later...

The tens can be done in the same fashion. Except this time you will want to make sure the number is 2 or higher. Since there is no "tenty" only "twenty", "thirty", "forty", etc...

For the ones position you can use the same logic you did with the hundreds except you don't tack on "hundred". If you make this a separate function you can use it for both hundreds and ones position. However you also need to see if the tens position is a "1". If it is you need to call a "0" a "ten", call a "1" an "eleven", etc.

That should take care of all numbers.

A switch statement looks like this:

Code:
                switch (x) {
                    case 1:
                        y = 3;
                        break;
                    case 2:
                        y = 4;
                        break;
                    default:
                        y = -1;
                        break;
                }
It's the same as:

Code:
                if (x == 1) {
                    y = 3;
                } else if (x == 2) {
                    y = 4;
                } else {
                    y = -1;
                }
I generally use if statements though since if your logic changes they are usually easier to add to. I haven't found a particular advantage to switch statements for what I've done.

Looking at your code above you might consider changing:
Code:
else if (x>=200 && x<300)
to:
Code:
else if (hundreds == 2)
Examples of solution, without a solution...

Code:
// three lines of your code
hundreds = x / 100;
tens = (x % 100) / 10;
ones = (x % 100)%10;

// something is missing here... exercise for you!
LowNumberPrint(hundreds, false);
System.out.println(" Hundred ");

if (tens >= 2) {
  // logic to print twenty, thirty, etc.
  if (ones > 0) {
    LowNumberPrint(ones, false);
  }
} else if (tens == 1) {
  LowNumberPrint(ones, true);
} else {
  LowNumberPrint(ones, false);
}
Code:
private static void LowNumberPrint(int iNumToPrint, boolean bIsTeens) {
  if (bIsTeens) {
    // print out teen numbers (10-19)
  } else {
    // print out ones numbers (0-9)
  }
}
Hopefully this gives you something to poke at... I didn't throw it into my compiler so hopefully it's close enough to understand. I'm sure somebody will find an error in the code but hey... that's what forums are for! :p
 
Comment

SilentPanda

Moderator emeritus
Oct 8, 2002
9,993
30
The Bamboo Forest
Of course if you're REALLY on a time crunch you could do....

Code:
if (x == 0) {
  System.out.println("Zero");
} else if (x == 1) {
  System.out.println("One");
}......... else if (x == 231) {
  System.out.println("Two Hundred Thirty One");
}......... else if (x == 999) {
  System.out.println("Nine Hundred Ninety Nine");
}
Although you might want to write a program to write the code for you! :)
 
Comment

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Ok. Back.

I completed the assignment. After you turn yours in and get a grade, come back and post here with your solution. I'll then also post mine and we can compare techniques.

I wrote a driver to test mine. Here's the driver so you can test yours before you turn it in. ;)

Code:
public class numtoword {
	public static void main(String[]args) {                     
		for (int i = 0 ; i < 1000 ; i++) { 
			numtoword2(i) ; 
		} 
	}
Here's some sample output to drool over.

Code:
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Eighteen
Nineteen
Twenty 
Twenty One
Twenty Two
Twenty Three
Twenty Four
Twenty Five
Twenty Six
Twenty Seven
Twenty Eight
Twenty Nine
Thirty 
Thirty One
Thirty Two
Thirty Three
Thirty Four
Thirty Five
Thirty Six
Thirty Seven
Thirty Eight
Thirty Nine
Forty 
Forty One
Forty Two
Forty Three
Forty Four
Forty Five
Forty Six
Forty Seven
Forty Eight
Forty Nine
Fifty 
Fifty One
Fifty Two
Fifty Three
Fifty Four
Fifty Five
Fifty Six
Fifty Seven
Fifty Eight
Fifty Nine
Sixty 
Sixty One
Sixty Two
Sixty Three
Sixty Four
Sixty Five
Sixty Six
Sixty Seven
Sixty Eight
Sixty Nine
Seventy 
Seventy One
Seventy Two
Seventy Three
Seventy Four
Seventy Five
Seventy Six
Seventy Seven
Seventy Eight
Seventy Nine
Eighty 
Eighty One
Eighty Two
Eighty Three
Eighty Four
Eighty Five
Eighty Six
Eighty Seven
Eighty Eight
Eighty Nine
Ninety 
Ninety One
Ninety Two
Ninety Three
Ninety Four
Ninety Five
Ninety Six
Ninety Seven
Ninety Eight
Ninety Nine
One Hundred 
One Hundred One
One Hundred Two
One Hundred Three
One Hundred Four
One Hundred Five
One Hundred Six
One Hundred Seven
One Hundred Eight
One Hundred Nine
One Hundred Ten
One Hundred Eleven
One Hundred Twelve
One Hundred Thirteen
One Hundred Fourteen
One Hundred Fifteen
One Hundred Sixteen
One Hundred Seventeen
One Hundred Eighteen
One Hundred Nineteen
One Hundred Twenty 
One Hundred Twenty One
One Hundred Twenty Two
One Hundred Twenty Three
One Hundred Twenty Four
One Hundred Twenty Five
One Hundred Twenty Six
One Hundred Twenty Seven
One Hundred Twenty Eight
One Hundred Twenty Nine
One Hundred Thirty 
One Hundred Thirty One
One Hundred Thirty Two
One Hundred Thirty Three
One Hundred Thirty Four
One Hundred Thirty Five
One Hundred Thirty Six
One Hundred Thirty Seven
One Hundred Thirty Eight
One Hundred Thirty Nine
One Hundred Forty 
One Hundred Forty One
One Hundred Forty Two
One Hundred Forty Three
One Hundred Forty Four
One Hundred Forty Five
One Hundred Forty Six
One Hundred Forty Seven
One Hundred Forty Eight
One Hundred Forty Nine
One Hundred Fifty 
One Hundred Fifty One
One Hundred Fifty Two
One Hundred Fifty Three
One Hundred Fifty Four
One Hundred Fifty Five
One Hundred Fifty Six
One Hundred Fifty Seven
One Hundred Fifty Eight
One Hundred Fifty Nine
One Hundred Sixty 
One Hundred Sixty One
One Hundred Sixty Two
One Hundred Sixty Three
One Hundred Sixty Four
One Hundred Sixty Five
One Hundred Sixty Six
One Hundred Sixty Seven
One Hundred Sixty Eight
One Hundred Sixty Nine
One Hundred Seventy 
One Hundred Seventy One
One Hundred Seventy Two
One Hundred Seventy Three
One Hundred Seventy Four
One Hundred Seventy Five
One Hundred Seventy Six
One Hundred Seventy Seven
One Hundred Seventy Eight
One Hundred Seventy Nine
One Hundred Eighty 
One Hundred Eighty One
One Hundred Eighty Two
One Hundred Eighty Three
One Hundred Eighty Four
One Hundred Eighty Five
One Hundred Eighty Six
One Hundred Eighty Seven
One Hundred Eighty Eight
One Hundred Eighty Nine
One Hundred Ninety 
One Hundred Ninety One
One Hundred Ninety Two
One Hundred Ninety Three
One Hundred Ninety Four
One Hundred Ninety Five
One Hundred Ninety Six
One Hundred Ninety Seven
One Hundred Ninety Eight
One Hundred Ninety Nine
Two Hundred 
Two Hundred One
Two Hundred Two
Two Hundred Three
Two Hundred Four
Two Hundred Five
Two Hundred Six
Two Hundred Seven
Two Hundred Eight
Two Hundred Nine
Two Hundred Ten
Two Hundred Eleven
Two Hundred Twelve
Two Hundred Thirteen
Two Hundred Fourteen
Two Hundred Fifteen
Two Hundred Sixteen
Two Hundred Seventeen
Two Hundred Eighteen
Two Hundred Nineteen
Two Hundred Twenty 
Two Hundred Twenty One
Two Hundred Twenty Two
Two Hundred Twenty Three
Two Hundred Twenty Four
Two Hundred Twenty Five
Two Hundred Twenty Six
Two Hundred Twenty Seven
Two Hundred Twenty Eight
Two Hundred Twenty Nine
Two Hundred Thirty 
Two Hundred Thirty One
Two Hundred Thirty Two
Two Hundred Thirty Three
Two Hundred Thirty Four
Two Hundred Thirty Five
Two Hundred Thirty Six
Two Hundred Thirty Seven
Two Hundred Thirty Eight
Two Hundred Thirty Nine
Two Hundred Forty 
Two Hundred Forty One
Two Hundred Forty Two
Two Hundred Forty Three
Two Hundred Forty Four
Two Hundred Forty Five
Two Hundred Forty Six
Two Hundred Forty Seven
Two Hundred Forty Eight
Two Hundred Forty Nine
Two Hundred Fifty 
Two Hundred Fifty One
Two Hundred Fifty Two
Two Hundred Fifty Three
Two Hundred Fifty Four
Two Hundred Fifty Five
Two Hundred Fifty Six
Two Hundred Fifty Seven
Two Hundred Fifty Eight
Two Hundred Fifty Nine
Two Hundred Sixty 
Two Hundred Sixty One
Two Hundred Sixty Two
Two Hundred Sixty Three
Two Hundred Sixty Four
Two Hundred Sixty Five
Two Hundred Sixty Six
Two Hundred Sixty Seven
Two Hundred Sixty Eight
Two Hundred Sixty Nine
Two Hundred Seventy 
Two Hundred Seventy One
Two Hundred Seventy Two
Two Hundred Seventy Three
Two Hundred Seventy Four
Two Hundred Seventy Five
Two Hundred Seventy Six
Two Hundred Seventy Seven
Two Hundred Seventy Eight
Two Hundred Seventy Nine
Two Hundred Eighty 
Two Hundred Eighty One
Two Hundred Eighty Two
Two Hundred Eighty Three
Two Hundred Eighty Four
Two Hundred Eighty Five
Two Hundred Eighty Six
Two Hundred Eighty Seven
Two Hundred Eighty Eight
Two Hundred Eighty Nine
Two Hundred Ninety 
Two Hundred Ninety One
Two Hundred Ninety Two
Two Hundred Ninety Three
Two Hundred Ninety Four
Two Hundred Ninety Five
Two Hundred Ninety Six
Two Hundred Ninety Seven
Two Hundred Ninety Eight
Two Hundred Ninety Nine
Three Hundred 
Three Hundred One
Three Hundred Two
Three Hundred Three
Three Hundred Four
Three Hundred Five
Three Hundred Six
Three Hundred Seven
Three Hundred Eight
Three Hundred Nine
Three Hundred Ten
Three Hundred Eleven
Three Hundred Twelve
Three Hundred Thirteen
Three Hundred Fourteen
Three Hundred Fifteen
Three Hundred Sixteen
Three Hundred Seventeen
Three Hundred Eighteen
Three Hundred Nineteen
Three Hundred Twenty 
Three Hundred Twenty One
Three Hundred Twenty Two
Three Hundred Twenty Three
Three Hundred Twenty Four
Three Hundred Twenty Five
Three Hundred Twenty Six
Three Hundred Twenty Seven
Three Hundred Twenty Eight
Three Hundred Twenty Nine
Three Hundred Thirty 
Three Hundred Thirty One
Three Hundred Thirty Two
Three Hundred Thirty Three
Three Hundred Thirty Four
Three Hundred Thirty Five
Three Hundred Thirty Six
Three Hundred Thirty Seven
Three Hundred Thirty Eight
Three Hundred Thirty Nine
Three Hundred Forty 
Three Hundred Forty One
Three Hundred Forty Two
Three Hundred Forty Three
Three Hundred Forty Four
Three Hundred Forty Five
Three Hundred Forty Six
Three Hundred Forty Seven
Three Hundred Forty Eight
Three Hundred Forty Nine
Three Hundred Fifty 
Three Hundred Fifty One
Three Hundred Fifty Two
Three Hundred Fifty Three
Three Hundred Fifty Four
Three Hundred Fifty Five
Three Hundred Fifty Six
Three Hundred Fifty Seven
Three Hundred Fifty Eight
Three Hundred Fifty Nine
Three Hundred Sixty 
Three Hundred Sixty One
Three Hundred Sixty Two
Three Hundred Sixty Three
Three Hundred Sixty Four
Three Hundred Sixty Five
Three Hundred Sixty Six
Three Hundred Sixty Seven
Three Hundred Sixty Eight
Three Hundred Sixty Nine
Three Hundred Seventy 
Three Hundred Seventy One
Three Hundred Seventy Two
Three Hundred Seventy Three
Three Hundred Seventy Four
Three Hundred Seventy Five
Three Hundred Seventy Six
Three Hundred Seventy Seven
Three Hundred Seventy Eight
Three Hundred Seventy Nine
Three Hundred Eighty 
Three Hundred Eighty One
Three Hundred Eighty Two
Three Hundred Eighty Three
Three Hundred Eighty Four
Three Hundred Eighty Five
Three Hundred Eighty Six
Three Hundred Eighty Seven
Three Hundred Eighty Eight
Three Hundred Eighty Nine
Three Hundred Ninety 
Three Hundred Ninety One
Three Hundred Ninety Two
Three Hundred Ninety Three
Three Hundred Ninety Four
Three Hundred Ninety Five
Three Hundred Ninety Six
Three Hundred Ninety Seven
Three Hundred Ninety Eight
Three Hundred Ninety Nine
Four Hundred 
Four Hundred One
Four Hundred Two
Four Hundred Three
Four Hundred Four
Four Hundred Five
Four Hundred Six
Four Hundred Seven
Four Hundred Eight
Four Hundred Nine
Four Hundred Ten
Four Hundred Eleven
Four Hundred Twelve
Four Hundred Thirteen
Four Hundred Fourteen
Four Hundred Fifteen
Four Hundred Sixteen
Four Hundred Seventeen
Four Hundred Eighteen
Four Hundred Nineteen
Four Hundred Twenty 
Four Hundred Twenty One
Four Hundred Twenty Two
Four Hundred Twenty Three
Four Hundred Twenty Four
Four Hundred Twenty Five
Four Hundred Twenty Six
Four Hundred Twenty Seven
Four Hundred Twenty Eight
Four Hundred Twenty Nine
Four Hundred Thirty 
Four Hundred Thirty One
Four Hundred Thirty Two
Four Hundred Thirty Three
Four Hundred Thirty Four
Four Hundred Thirty Five
Four Hundred Thirty Six
Four Hundred Thirty Seven
Four Hundred Thirty Eight
Four Hundred Thirty Nine
Four Hundred Forty 
Four Hundred Forty One
Four Hundred Forty Two
Four Hundred Forty Three
Four Hundred Forty Four
Four Hundred Forty Five
Four Hundred Forty Six
Four Hundred Forty Seven
Four Hundred Forty Eight
Four Hundred Forty Nine
Four Hundred Fifty 
Four Hundred Fifty One
Four Hundred Fifty Two
Four Hundred Fifty Three
Four Hundred Fifty Four
Four Hundred Fifty Five
Four Hundred Fifty Six
Four Hundred Fifty Seven
Four Hundred Fifty Eight
Four Hundred Fifty Nine
Four Hundred Sixty 
Four Hundred Sixty One
Four Hundred Sixty Two
Four Hundred Sixty Three
Four Hundred Sixty Four
Four Hundred Sixty Five
Four Hundred Sixty Six
Four Hundred Sixty Seven
Four Hundred Sixty Eight
Four Hundred Sixty Nine
Four Hundred Seventy 
Four Hundred Seventy One
Four Hundred Seventy Two
Four Hundred Seventy Three
Four Hundred Seventy Four
Four Hundred Seventy Five
Four Hundred Seventy Six
Four Hundred Seventy Seven
Four Hundred Seventy Eight
Four Hundred Seventy Nine
Four Hundred Eighty 
Four Hundred Eighty One
Four Hundred Eighty Two
Four Hundred Eighty Three
Four Hundred Eighty Four
Four Hundred Eighty Five
Four Hundred Eighty Six
Four Hundred Eighty Seven
Four Hundred Eighty Eight
Four Hundred Eighty Nine
Four Hundred Ninety 
Four Hundred Ninety One
Four Hundred Ninety Two
Four Hundred Ninety Three
Four Hundred Ninety Four
Four Hundred Ninety Five
Four Hundred Ninety Six
Four Hundred Ninety Seven
Four Hundred Ninety Eight
Four Hundred Ninety Nine
Five Hundred 
Five Hundred One
Five Hundred Two
Five Hundred Three
Five Hundred Four
Five Hundred Five
Five Hundred Six
Five Hundred Seven
Five Hundred Eight
Five Hundred Nine
Five Hundred Ten
Five Hundred Eleven
Five Hundred Twelve
Five Hundred Thirteen
Five Hundred Fourteen
Five Hundred Fifteen
Five Hundred Sixteen
Five Hundred Seventeen
Five Hundred Eighteen
Five Hundred Nineteen
Five Hundred Twenty 
Five Hundred Twenty One
Five Hundred Twenty Two
Five Hundred Twenty Three
Five Hundred Twenty Four
Five Hundred Twenty Five
Five Hundred Twenty Six
Five Hundred Twenty Seven
Five Hundred Twenty Eight
Five Hundred Twenty Nine
Five Hundred Thirty 
Five Hundred Thirty One
Five Hundred Thirty Two
Five Hundred Thirty Three
Five Hundred Thirty Four
Five Hundred Thirty Five
Five Hundred Thirty Six
Five Hundred Thirty Seven
Five Hundred Thirty Eight
Five Hundred Thirty Nine
Five Hundred Forty 
Five Hundred Forty One
Five Hundred Forty Two
Five Hundred Forty Three
Five Hundred Forty Four
Five Hundred Forty Five
Five Hundred Forty Six
Five Hundred Forty Seven
Five Hundred Forty Eight
Five Hundred Forty Nine
Five Hundred Fifty 
Five Hundred Fifty One
Five Hundred Fifty Two
Five Hundred Fifty Three
Five Hundred Fifty Four
Five Hundred Fifty Five
Five Hundred Fifty Six
Five Hundred Fifty Seven
Five Hundred Fifty Eight
Five Hundred Fifty Nine
Five Hundred Sixty 
Five Hundred Sixty One
Five Hundred Sixty Two
Five Hundred Sixty Three
Five Hundred Sixty Four
Five Hundred Sixty Five
Five Hundred Sixty Six
Five Hundred Sixty Seven
Five Hundred Sixty Eight
Five Hundred Sixty Nine
Five Hundred Seventy 
Five Hundred Seventy One
Five Hundred Seventy Two
Five Hundred Seventy Three
Five Hundred Seventy Four
Five Hundred Seventy Five
Five Hundred Seventy Six
Five Hundred Seventy Seven
Five Hundred Seventy Eight
Five Hundred Seventy Nine
Five Hundred Eighty 
Five Hundred Eighty One
Five Hundred Eighty Two
Five Hundred Eighty Three
Five Hundred Eighty Four
Five Hundred Eighty Five
Five Hundred Eighty Six
Five Hundred Eighty Seven
Five Hundred Eighty Eight
Five Hundred Eighty Nine
Five Hundred Ninety 
Five Hundred Ninety One
Five Hundred Ninety Two
Five Hundred Ninety Three
Five Hundred Ninety Four
Five Hundred Ninety Five
Five Hundred Ninety Six
Five Hundred Ninety Seven
Five Hundred Ninety Eight
Five Hundred Ninety Nine
Six Hundred 
Six Hundred One
Six Hundred Two
Six Hundred Three
Six Hundred Four
Six Hundred Five
Six Hundred Six
Six Hundred Seven
Six Hundred Eight
Six Hundred Nine
Six Hundred Ten
Six Hundred Eleven
Six Hundred Twelve
Six Hundred Thirteen
Six Hundred Fourteen
Six Hundred Fifteen
Six Hundred Sixteen
Six Hundred Seventeen
Six Hundred Eighteen
Six Hundred Nineteen
Six Hundred Twenty 
Six Hundred Twenty One
Six Hundred Twenty Two
Six Hundred Twenty Three
Six Hundred Twenty Four
Six Hundred Twenty Five
Six Hundred Twenty Six
Six Hundred Twenty Seven
Six Hundred Twenty Eight
Six Hundred Twenty Nine
Six Hundred Thirty 
Six Hundred Thirty One
Six Hundred Thirty Two
Six Hundred Thirty Three
Six Hundred Thirty Four
Six Hundred Thirty Five
Six Hundred Thirty Six
Six Hundred Thirty Seven
Six Hundred Thirty Eight
Six Hundred Thirty Nine
Six Hundred Forty 
Six Hundred Forty One
Six Hundred Forty Two
Six Hundred Forty Three
Six Hundred Forty Four
Six Hundred Forty Five
Six Hundred Forty Six
Six Hundred Forty Seven
Six Hundred Forty Eight
Six Hundred Forty Nine
Six Hundred Fifty 
Six Hundred Fifty One
Six Hundred Fifty Two
Six Hundred Fifty Three
Six Hundred Fifty Four
Six Hundred Fifty Five
Six Hundred Fifty Six
Six Hundred Fifty Seven
Six Hundred Fifty Eight
Six Hundred Fifty Nine
Six Hundred Sixty 
Six Hundred Sixty One
Six Hundred Sixty Two
Six Hundred Sixty Three
Six Hundred Sixty Four
Six Hundred Sixty Five
Six Hundred Sixty Six
Six Hundred Sixty Seven
Six Hundred Sixty Eight
Six Hundred Sixty Nine
Six Hundred Seventy 
Six Hundred Seventy One
Six Hundred Seventy Two
Six Hundred Seventy Three
Six Hundred Seventy Four
Six Hundred Seventy Five
Six Hundred Seventy Six
Six Hundred Seventy Seven
Six Hundred Seventy Eight
Six Hundred Seventy Nine
Six Hundred Eighty 
Six Hundred Eighty One
Six Hundred Eighty Two
Six Hundred Eighty Three
Six Hundred Eighty Four
Six Hundred Eighty Five
Six Hundred Eighty Six
Six Hundred Eighty Seven
Six Hundred Eighty Eight
Six Hundred Eighty Nine
Six Hundred Ninety 
Six Hundred Ninety One
Six Hundred Ninety Two
Six Hundred Ninety Three
Six Hundred Ninety Four
Six Hundred Ninety Five
Six Hundred Ninety Six
Six Hundred Ninety Seven
Six Hundred Ninety Eight
Six Hundred Ninety Nine
Seven Hundred 
Seven Hundred One
Seven Hundred Two
Seven Hundred Three
Seven Hundred Four
Seven Hundred Five
Seven Hundred Six
Seven Hundred Seven
Seven Hundred Eight
Seven Hundred Nine
Seven Hundred Ten
Seven Hundred Eleven
Seven Hundred Twelve
Seven Hundred Thirteen
Seven Hundred Fourteen
Seven Hundred Fifteen
Seven Hundred Sixteen
Seven Hundred Seventeen
Seven Hundred Eighteen
Seven Hundred Nineteen
Seven Hundred Twenty 
Seven Hundred Twenty One
Seven Hundred Twenty Two
Seven Hundred Twenty Three
Seven Hundred Twenty Four
Seven Hundred Twenty Five
Seven Hundred Twenty Six
Seven Hundred Twenty Seven
Seven Hundred Twenty Eight
Seven Hundred Twenty Nine
Seven Hundred Thirty 
Seven Hundred Thirty One
Seven Hundred Thirty Two
Seven Hundred Thirty Three
Seven Hundred Thirty Four
Seven Hundred Thirty Five
Seven Hundred Thirty Six
Seven Hundred Thirty Seven
Seven Hundred Thirty Eight
Seven Hundred Thirty Nine
Seven Hundred Forty 
Seven Hundred Forty One
Seven Hundred Forty Two
Seven Hundred Forty Three
Seven Hundred Forty Four
Seven Hundred Forty Five
Seven Hundred Forty Six
Seven Hundred Forty Seven
Seven Hundred Forty Eight
Seven Hundred Forty Nine
Seven Hundred Fifty 
Seven Hundred Fifty One
Seven Hundred Fifty Two
Seven Hundred Fifty Three
Seven Hundred Fifty Four
Seven Hundred Fifty Five
Seven Hundred Fifty Six
Seven Hundred Fifty Seven
Seven Hundred Fifty Eight
Seven Hundred Fifty Nine
Seven Hundred Sixty 
Seven Hundred Sixty One
Seven Hundred Sixty Two
Seven Hundred Sixty Three
Seven Hundred Sixty Four
Seven Hundred Sixty Five
Seven Hundred Sixty Six
Seven Hundred Sixty Seven
Seven Hundred Sixty Eight
Seven Hundred Sixty Nine
Seven Hundred Seventy 
Seven Hundred Seventy One
Seven Hundred Seventy Two
Seven Hundred Seventy Three
Seven Hundred Seventy Four
Seven Hundred Seventy Five
Seven Hundred Seventy Six
Seven Hundred Seventy Seven
Seven Hundred Seventy Eight
Seven Hundred Seventy Nine
Seven Hundred Eighty 
Seven Hundred Eighty One
Seven Hundred Eighty Two
Seven Hundred Eighty Three
Seven Hundred Eighty Four
Seven Hundred Eighty Five
Seven Hundred Eighty Six
Seven Hundred Eighty Seven
Seven Hundred Eighty Eight
Seven Hundred Eighty Nine
Seven Hundred Ninety 
Seven Hundred Ninety One
Seven Hundred Ninety Two
Seven Hundred Ninety Three
Seven Hundred Ninety Four
Seven Hundred Ninety Five
Seven Hundred Ninety Six
Seven Hundred Ninety Seven
Seven Hundred Ninety Eight
Seven Hundred Ninety Nine
Eight Hundred 
Eight Hundred One
Eight Hundred Two
Eight Hundred Three
Eight Hundred Four
Eight Hundred Five
Eight Hundred Six
Eight Hundred Seven
Eight Hundred Eight
Eight Hundred Nine
Eight Hundred Ten
Eight Hundred Eleven
Eight Hundred Twelve
Eight Hundred Thirteen
Eight Hundred Fourteen
Eight Hundred Fifteen
Eight Hundred Sixteen
Eight Hundred Seventeen
Eight Hundred Eighteen
Eight Hundred Nineteen
Eight Hundred Twenty 
Eight Hundred Twenty One
Eight Hundred Twenty Two
Eight Hundred Twenty Three
Eight Hundred Twenty Four
Eight Hundred Twenty Five
Eight Hundred Twenty Six
Eight Hundred Twenty Seven
Eight Hundred Twenty Eight
Eight Hundred Twenty Nine
Eight Hundred Thirty 
Eight Hundred Thirty One
Eight Hundred Thirty Two
Eight Hundred Thirty Three
Eight Hundred Thirty Four
Eight Hundred Thirty Five
Eight Hundred Thirty Six
Eight Hundred Thirty Seven
Eight Hundred Thirty Eight
Eight Hundred Thirty Nine
Eight Hundred Forty 
Eight Hundred Forty One
Eight Hundred Forty Two
Eight Hundred Forty Three
Eight Hundred Forty Four
Eight Hundred Forty Five
Eight Hundred Forty Six
Eight Hundred Forty Seven
Eight Hundred Forty Eight
Eight Hundred Forty Nine
Eight Hundred Fifty 
Eight Hundred Fifty One
Eight Hundred Fifty Two
Eight Hundred Fifty Three
Eight Hundred Fifty Four
Eight Hundred Fifty Five
Eight Hundred Fifty Six
Eight Hundred Fifty Seven
Eight Hundred Fifty Eight
Eight Hundred Fifty Nine
Eight Hundred Sixty 
Eight Hundred Sixty One
Eight Hundred Sixty Two
Eight Hundred Sixty Three
Eight Hundred Sixty Four
Eight Hundred Sixty Five
Eight Hundred Sixty Six
Eight Hundred Sixty Seven
Eight Hundred Sixty Eight
Eight Hundred Sixty Nine
Eight Hundred Seventy 
Eight Hundred Seventy One
Eight Hundred Seventy Two
Eight Hundred Seventy Three
Eight Hundred Seventy Four
Eight Hundred Seventy Five
Eight Hundred Seventy Six
Eight Hundred Seventy Seven
Eight Hundred Seventy Eight
Eight Hundred Seventy Nine
Eight Hundred Eighty 
Eight Hundred Eighty One
Eight Hundred Eighty Two
Eight Hundred Eighty Three
Eight Hundred Eighty Four
Eight Hundred Eighty Five
Eight Hundred Eighty Six
Eight Hundred Eighty Seven
Eight Hundred Eighty Eight
Eight Hundred Eighty Nine
Eight Hundred Ninety 
Eight Hundred Ninety One
Eight Hundred Ninety Two
Eight Hundred Ninety Three
Eight Hundred Ninety Four
Eight Hundred Ninety Five
Eight Hundred Ninety Six
Eight Hundred Ninety Seven
Eight Hundred Ninety Eight
Eight Hundred Ninety Nine
Nine Hundred 
Nine Hundred One
Nine Hundred Two
Nine Hundred Three
Nine Hundred Four
Nine Hundred Five
Nine Hundred Six
Nine Hundred Seven
Nine Hundred Eight
Nine Hundred Nine
Nine Hundred Ten
Nine Hundred Eleven
Nine Hundred Twelve
Nine Hundred Thirteen
Nine Hundred Fourteen
Nine Hundred Fifteen
Nine Hundred Sixteen
Nine Hundred Seventeen
Nine Hundred Eighteen
Nine Hundred Nineteen
Nine Hundred Twenty 
Nine Hundred Twenty One
Nine Hundred Twenty Two
Nine Hundred Twenty Three
Nine Hundred Twenty Four
Nine Hundred Twenty Five
Nine Hundred Twenty Six
Nine Hundred Twenty Seven
Nine Hundred Twenty Eight
Nine Hundred Twenty Nine
Nine Hundred Thirty 
Nine Hundred Thirty One
Nine Hundred Thirty Two
Nine Hundred Thirty Three
Nine Hundred Thirty Four
Nine Hundred Thirty Five
Nine Hundred Thirty Six
Nine Hundred Thirty Seven
Nine Hundred Thirty Eight
Nine Hundred Thirty Nine
Nine Hundred Forty 
Nine Hundred Forty One
Nine Hundred Forty Two
Nine Hundred Forty Three
Nine Hundred Forty Four
Nine Hundred Forty Five
Nine Hundred Forty Six
Nine Hundred Forty Seven
Nine Hundred Forty Eight
Nine Hundred Forty Nine
Nine Hundred Fifty 
Nine Hundred Fifty One
Nine Hundred Fifty Two
Nine Hundred Fifty Three
Nine Hundred Fifty Four
Nine Hundred Fifty Five
Nine Hundred Fifty Six
Nine Hundred Fifty Seven
Nine Hundred Fifty Eight
Nine Hundred Fifty Nine
Nine Hundred Sixty 
Nine Hundred Sixty One
Nine Hundred Sixty Two
Nine Hundred Sixty Three
Nine Hundred Sixty Four
Nine Hundred Sixty Five
Nine Hundred Sixty Six
Nine Hundred Sixty Seven
Nine Hundred Sixty Eight
Nine Hundred Sixty Nine
Nine Hundred Seventy 
Nine Hundred Seventy One
Nine Hundred Seventy Two
Nine Hundred Seventy Three
Nine Hundred Seventy Four
Nine Hundred Seventy Five
Nine Hundred Seventy Six
Nine Hundred Seventy Seven
Nine Hundred Seventy Eight
Nine Hundred Seventy Nine
Nine Hundred Eighty 
Nine Hundred Eighty One
Nine Hundred Eighty Two
Nine Hundred Eighty Three
Nine Hundred Eighty Four
Nine Hundred Eighty Five
Nine Hundred Eighty Six
Nine Hundred Eighty Seven
Nine Hundred Eighty Eight
Nine Hundred Eighty Nine
Nine Hundred Ninety 
Nine Hundred Ninety One
Nine Hundred Ninety Two
Nine Hundred Ninety Three
Nine Hundred Ninety Four
Nine Hundred Ninety Five
Nine Hundred Ninety Six
Nine Hundred Ninety Seven
Nine Hundred Ninety Eight
Nine Hundred Ninety Nine
 
Comment

AJ Muni

macrumors 65816
Original poster
Aug 4, 2005
1,145
17
Miami
todd said:
Ok. Back.

I completed the assignment. After you turn yours in and get a grade, come back and post here with your solution. I'll then also post mine and we can compare techniques.

I wrote a driver to test mine. Here's the driver so you can test yours before you turn it in.
hey todd, i really appreciate your help. I saw what u did, but our teacher specifically said he didnt want 999 different statements. This is what i got so far..it works for all numbers, but doesnt for the teens. like 526, 786, 502, 145 all work fine, but 112, 219, 814, it doesnt work. check it out...

Code:
import java.util.Scanner;
   public class numtoword
  {
      public static void main(String[]args)
     {
       int hundreds;
       int tens;
       int ones;
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter number:  ");
       int x = scan.nextInt();

          hundreds = x / 100;
          tens = (x % 100) / 10;
          ones = (x % 100)%10;

           //Ones
           if (x==0){
           System.out.println("zero");

           if  (x>99 && x<200)
           System.out.print("One Hundred");
           if (x>=200 && x<300)
           System.out.print("Two hundred");
           if (x>=200 && x<300)
           System.out.print("Three hundred");
           if (x>=200 && x<300)
           System.out.print("four hundred");
           if (x>=200 && x<300)
           System.out.print("five hundred");
           if (x>=200 && x<300)
           System.out.print("six hundred");
           if (x>=200 && x<300)
           System.out.print("seven hundred");
           if (x>=200 && x<300)
           System.out.print("eight hundred");
           if (x>=200 && x<300)
           System.out.print("nine hundred");
           }

            //Switches
            switch (hundreds)
            {
            case 1:  System.out.print("One hundred "); break;
            case 2:  System.out.print("Two hundred "); break;
            case 3:  System.out.print("Three hundred "); break;
            case 4:  System.out.print("Four hundred "); break;
            case 5:  System.out.print("Five hundred "); break;
            case 6:  System.out.print("Six hundred "); break;
            case 7:  System.out.print("Seven hundred "); break;
            case 8:  System.out.print("Eight hundred "); break;
            case 9:  System.out.print("Nine hundred "); break;
            default: System.out.print("");break;
            }

             if (tens>=10 && tens<20)
              switch (tens)
              {
                 case 0:System.out.print(""); break;
                 case 1: System.out.print("eleven"); break;
                 case 2: System.out.print("twelve"); break;
                 case 3: System.out.print("thirteen"); break;
                 case 4: System.out.print("fourteen"); break;
                 case 5: System.out.print("fifteen"); break;
                 case 6: System.out.print("sixteen"); break;
                 case 7:System.out.print("seventeen"); break;
                 case 8: System.out.print("eighteen"); break;
                 case 9: System.out.print("nineteen"); break;
                 default: System.out.print(""); break;
              }

               
                else 
                switch (tens)
               {
           case 2: System.out.print("twenty"); break;
           case 3: System.out.print("thirty"); break;
           case 4: System.out.print("fourty"); break;
           case 5: System.out.print("fifty"); break;
           case 6: System.out.print("sixty"); break;
           case 7: System.out.print("seventy"); break;
           case 8: System.out.print("eighty"); break;
           case 9: System.out.print("ninety"); break;
           default: System.out.print(""); break;
               }

           if (ones>=1 && ones<10)
           switch (ones)
           {
           case 1: System.out.println(" one"); break;
           case 2: System.out.println(" two"); break;
           case 3: System.out.println(" three"); break;
           case 4: System.out.println(" four"); break;
           case 5: System.out.println(" five"); break;
           case 6: System.out.println(" six"); break;
           case 7: System.out.println(" seven"); break;
           case 8: System.out.println(" eight"); break;
           case 9: System.out.println(" nine"); break;
           default: System.out.println(""); break;
          }
        }
      }
 
Comment

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
I've a couple questions...

For this IF block, when will x be > 99 inside the block?

Code:
          //Ones
           if (x==0){
           System.out.println("zero");

           if  (x>99 && x<200)
           System.out.print("One Hundred");
           if (x>=200 && x<300)
           System.out.print("Two hundred");
           if (x>=200 && x<300)
           System.out.print("Three hundred");
           if (x>=200 && x<300)
           System.out.print("four hundred");
           if (x>=200 && x<300)
           System.out.print("five hundred");
           if (x>=200 && x<300)
           System.out.print("six hundred");
           if (x>=200 && x<300)
           System.out.print("seven hundred");
           if (x>=200 && x<300)
           System.out.print("eight hundred");
           if (x>=200 && x<300)
           System.out.print("nine hundred");
           }
And, similarly for this switch block, when will CASE 0: through CASE 9: ever hit?

Code:
            if (tens>=10 && tens<20)
              switch (tens)
              {
                 case 0:System.out.print(""); break;
                 case 1: System.out.print("eleven"); break;
                 case 2: System.out.print("twelve"); break;
                 case 3: System.out.print("thirteen"); break;
                 case 4: System.out.print("fourteen"); break;
                 case 5: System.out.print("fifteen"); break;
                 case 6: System.out.print("sixteen"); break;
                 case 7:System.out.print("seventeen"); break;
                 case 8: System.out.print("eighteen"); break;
                 case 9: System.out.print("nineteen"); break;
                 default: System.out.print(""); break;
              }
Todd
 
Comment

AJ Muni

macrumors 65816
Original poster
Aug 4, 2005
1,145
17
Miami
I've a couple questions...

For this IF block, when will x be > 99 inside the block?

Code:
          //Ones
           if (x==0){
           System.out.println("zero");

           if  (x>99 && x<200)
           System.out.print("One Hundred");
           if (x>=200 && x<300)
           System.out.print("Two hundred");
           if (x>=200 && x<300)
           System.out.print("Three hundred");
           if (x>=200 && x<300)
           System.out.print("four hundred");
           if (x>=200 && x<300)
           System.out.print("five hundred");
           if (x>=200 && x<300)
           System.out.print("six hundred");
           if (x>=200 && x<300)
           System.out.print("seven hundred");
           if (x>=200 && x<300)
           System.out.print("eight hundred");
           if (x>=200 && x<300)
           System.out.print("nine hundred");
           }
And, similarly for this switch block, when will CASE 0: through CASE 9: ever hit?

Code:
            if (tens>=10 && tens<20)
              switch (tens)
              {
                 case 0:System.out.print(""); break;
                 case 1: System.out.print("eleven"); break;
                 case 2: System.out.print("twelve"); break;
                 case 3: System.out.print("thirteen"); break;
                 case 4: System.out.print("fourteen"); break;
                 case 5: System.out.print("fifteen"); break;
                 case 6: System.out.print("sixteen"); break;
                 case 7:System.out.print("seventeen"); break;
                 case 8: System.out.print("eighteen"); break;
                 case 9: System.out.print("nineteen"); break;
                 default: System.out.print(""); break;
              }
Todd

for the first..numbers 100-199

and the second, all the teens 11,12,13...19
 
Comment

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,745
3,849
Los Angeles
I had this very problem on a final exam. I wrote code matching what the professor had in mind, while one of my classmates wrote the 1000-case-statement version, putting "..." in the middle rather than including the actual code!

I got a better grade on the test than my classmate did, even though his code was easier to read, and its correctness was easier to confirm! So what if it wouldn't actually compile and run?
 
Comment

SilentPanda

Moderator emeritus
Oct 8, 2002
9,993
30
The Bamboo Forest
I've a couple questions...

For this IF block, when will x be > 99 inside the block?

Code:
          //Ones
           if (x==0){
           System.out.println("zero");

           if  (x>99 && x<200)
           System.out.print("One Hundred");
           if (x>=200 && x<300)
           System.out.print("Two hundred");
           if (x>=200 && x<300)
           System.out.print("Three hundred");
           if (x>=200 && x<300)
           System.out.print("four hundred");
           if (x>=200 && x<300)
           System.out.print("five hundred");
           if (x>=200 && x<300)
           System.out.print("six hundred");
           if (x>=200 && x<300)
           System.out.print("seven hundred");
           if (x>=200 && x<300)
           System.out.print("eight hundred");
           if (x>=200 && x<300)
           System.out.print("nine hundred");
           }
x can be any number... so I think this is okay. The 2nd part you pointed out is correct though.

If you look at my posts up above which were just recently added back you might see some more help too.
 
Comment

SilentPanda

Moderator emeritus
Oct 8, 2002
9,993
30
The Bamboo Forest
x cannot be any value to enter the IF block. Only when (x==0) will the IF block be entered. And then, it will NEVER match any of the nested conditions (which, are pointless anyway).

Todd
You're right. I didn't see the {. I'm used to looking at my own code (I always use {}) and since there weren't any in the other if statements I didn't notice it in the first one. Good catch!
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.