# Help: Num to words

• December 31st, 2009, 06:58 PM
shamed
Help: Num to words
What shall I do to this program? It does not output correct.

When I input "6412" it will just output "Six Thousand Four Hundred Two"
When I input "17" It will only output "Ten Seven"
When I input "6433" it will just output "Six Thousand Four Hundred Three"

Why is it so? It supposedly be like: "17" should be "Seventeen", "12" should be "Twelve", "33" should be "Thirty Three" but still does not output correct. :(

Here is my code:
Code :

```import java.io.*;   public class NewClass {   public static void main(String[]args) throws IOException {   BufferedReader input=new BufferedReader(new InputStreamReader(System.in));   String o="Y"; String x;   do {String ans="",onez="",tenz="",hundredz="",thousandz="";   try { System.out.println("Enter a number from 1 to 9999:"); x=input.readLine(); int num=Integer.parseInt(x); int thousands=num/1000; int hundreds=(num/100)%10; int tens=(num-(hundreds*100))/10; int one=num%10;   if (num>=1&&num<=9999) {   switch(thousands){case 1:thousandz="One Thousand ";break; case 2:thousandz="Two Thousand ";break; case 3:thousandz="Three Thousand ";break; case 4:thousandz="Four Thousand ";break; case 5:thousandz="Five Thousand ";break; case 6:thousandz="Six Thousand ";break; case 7:thousandz="Seven Thousand ";break; case 8:thousandz="Eight Thousand ";break; case 9:thousandz="Nine Thousand ";break; }   switch(hundreds) {case 1: hundredz="One Hundred ";break; case 2: hundredz="Two Hundred ";break; case 3: hundredz="Three Hundred ";break; case 4: hundredz="Four Hundred ";break; case 5: hundredz="Five Hundred ";break; case 6: hundredz="Six Hundred ";break; case 7: hundredz="Seven Hundred ";break; case 8: hundredz="Eight Hundred ";break; case 9: hundredz="Nine Hundred ";break;}   switch(tens) {case 1:tenz="Ten ";break; case 2:tenz="Twenty ";break; case 3:tenz="Thirty ";break; case 4:tenz="Forty ";break; case 5:tenz="Fifty ";break; case 6:tenz="Sixty ";break; case 7:tenz="Seventy ";break; case 8:tenz="Eigthty ";break; case 9:tenz="Ninety ";break; case 11:tenz="Eleven";break; case 12:tenz="Twelve";break; case 13:tenz="Thirteen";break; case 14:tenz="Fourteen";break; case 15:tenz="Fifteen";break; case 16:tenz="Sixteen";break; case 17:tenz="Seventeen";break; case 18:tenz="Eighteen";break; case 19:tenz="Nineteen";break; }   switch(one) {case 1:onez="One";break; case 2:onez="Two";break; case 3:onez="Three";break; case 4:onez="Four";break; case 5:onez="Five";break; case 6:onez="Six";break; case 7:onez="Seven";break; case 8:onez="Eight";break; case 9:onez="Nine";break; }   ans=thousandz+hundredz+tenz+onez; System.out.println("Your input is "+ans); System.out.println("Continue?"); System.out.println("Press Y if YES, N if NO."); o=input.readLine(); }   else { System.out.println("invalid range"); }   }   catch(NumberFormatException ae) { System.out.println("invalid input"); System.out.println("enter another?"); System.out.println("Press Y if YES, N if NO.");; o = input.readLine(); }   }   while(o.equals ("y") || o.equals ("Y")); } }```
• January 1st, 2010, 12:01 AM
helloworld922
Re: Help: Num to words
Mmm... I had code to do just this somewhere :P

In your code where you're checking tenz, you need to add extra code when tenz == 1. It should return the appropriate string depending on what the one's unit is. Also, as a special note if you do happen to have tenz unit is 1, since you're already handling the units, you'll need to make sure the section of code that handles the units digits doesn't get executed, or you'll get stuff like "eleven one", "sixteen six" etc. etc.

Code :

```switch(tenz) { case 1: switch (onez) { case 0: // "ten" case 1: // "eleven" //... etc etc } case 2: // ... etc etc }```
• January 1st, 2010, 06:28 PM
shamed
Re: Help: Num to words
Hi, thank you for your reply, I picked an idea. But do you think the code I added is the best solution for the "two digit input"?..

Code :

```try { System.out.println("Enter a number from 1 to 9999:"); x=input.readLine(); int num=Integer.parseInt(x); int thousands=num/1000; int hundreds=(num/100)%10; int tens=(num-(hundreds*100))/10; int one=num%10;   if (num>=1&&num<=9999) {   switch(thousands){ case 1:thousandz="One Thousand ";break; case 2:thousandz="Two Thousand ";break; case 3:thousandz="Three Thousand ";break; case 4:thousandz="Four Thousand ";break; case 5:thousandz="Five Thousand ";break; case 6:thousandz="Six Thousand ";break; case 7:thousandz="Seven Thousand ";break; case 8:thousandz="Eight Thousand ";break; case 9:thousandz="Nine Thousand ";break; }   switch(hundreds) { case 1: hundredz="One Hundred ";break; case 2: hundredz="Two Hundred ";break; case 3: hundredz="Three Hundred ";break; case 4: hundredz="Four Hundred ";break; case 5: hundredz="Five Hundred ";break; case 6: hundredz="Six Hundred ";break; case 7: hundredz="Seven Hundred ";break; case 8: hundredz="Eight Hundred ";break; case 9: hundredz="Nine Hundred ";break; } if (tens>1) { switch(tens) { case 1:tenz="Ten ";break; case 2:tenz="Twenty ";break; case 3:tenz="Thirty ";break; case 4:tenz="Forty ";break; case 5:tenz="Fifty ";break; case 6:tenz="Sixty ";break; case 7:tenz="Seventy ";break; case 8:tenz="Eigthty ";break; case 9:tenz="Ninety ";break; } switch (one) { case 1:onez="One";break; case 2:onez="Two";break; case 3:onez="Three";break; case 4:onez="Four";break; case 5:onez="Five";break; case 6:onez="Six";break; case 7:onez="Seven";break; case 8:onez="Eight";break; case 9:onez="Nine";break; } }   if (tens==1) { switch(one) { case 0:tenz="Ten";break; case 1:tenz="Eleven";break; case 2:tenz="Twelve";break; case 3:tenz="Thirteen";break; case 4:tenz="Fourteen";break; case 5:tenz="Fiften";break; case 6:tenz="Sixteen";break; case 7:tenz="Seventeen";break; case 8:tenz="Eighteen";break; case 9:tenz="Nineteen";break; } }   else if (tens==0) { switch(one) { case 1:onez="One";break; case 2:onez="Two";break; case 3:onez="Three";break; case 4:onez="Four";break; case 5:onez="Five";break; case 6:onez="Six";break; case 7:onez="Seven";break; case 8:onez="Eight";break; case 9:onez="Nine";break; } }   ans=thousandz+hundredz+tenz+onez; System.out.println("Your input is "+ans); System.out.println("Continue?"); System.out.println("Press Y if YES, N if NO."); o=input.readLine(); }   else { System.out.println("invalid range"); }   }   catch(NumberFormatException ae) { System.out.println("invalid input"); System.out.println("enter another?"); System.out.println("Press Y if YES, N if NO.");; o = input.readLine(); }```

My previous code does not display the right output when a user enters 2-digit number. When the user enters 12, the display is TenTwo.

But this one, it displays correct now. 12 is now Twelve, 22 is now Twenty Two.

My only problem now is when I input 4-digit number, let's say I'll have 2323 as an input. The display will be "Two Thousand Three Hundred Three". :(
• January 1st, 2010, 07:07 PM
helloworld922
Re: Help: Num to words
The way you're calculating the tens digit isn't giving back the right value for large numbers.. try this:

Code :

`tens = (num/10) %10;`
• January 6th, 2010, 03:58 PM
shamed
Re: Help: Num to words
Hi, thank you very much for your help! It did run.

But I was not able to see that the problem was "the output must also accept negative numbers"

This goes for example -4522 turns "Negative Four Thousand Five Hundred Twenty Two"

I did:
Code :

`((in>=1&&in<=9999) || (in>=-9999||in<=1))`

but it does not output correct again :(
• January 6th, 2010, 06:44 PM
helloworld922
Re: Help: Num to words
I would check the negative part first:

Code :

```if (num < 0) { str += "Negative "; // or what-ever the string your returning is called num = -num; // get positive num so rest of code works as planned } // .. rest of code to handle positive numbers```
• January 7th, 2010, 03:40 PM
shamed
Re: Help: Num to words
Quote:

Originally Posted by helloworld922
I would check the negative part first:

Code :

```if (num < 0) { str += "Negative "; // or what-ever the string your returning is called num = -num; // get positive num so rest of code works as planned } // .. rest of code to handle positive numbers```

I don't get it sir :( sorry..
• January 7th, 2010, 05:55 PM
helloworld922
Re: Help: Num to words
All the code you had before handled positive numbers, correct? So, if it's negative, simply add "negative" to the beginning of the string and do the num-word stuff on the absolute value of the number (aka. the positive part).

A simply way to check if a number is negative is to simply see if it's less than 0 :)