# Help: Num to words

• 01-01-2010, 02:48 AM
MyOnlineChurva
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"

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"));     } }```
"17" should be "Seventeen", "12" should be "Twelve", "33" should be "Thirty Three" but it does not output correct.
• 01-01-2010, 03:42 AM
Eranga
Think about your logic with switch-case. I hope you know the way it works, once a break statement found VM comes out of the switch-case.

If you enter number 12 as an example, you decomposed it into "Ten" and "Two". Did you found that?
• 01-01-2010, 04:02 AM
Fubarable
Also consider using arrays here to simplify things greatly.
• 01-01-2010, 04:31 AM
MyOnlineChurva
I thought my logic was right. I don't get what's wrong with it. I've already transferred this:
Code:

```                      case 10:onez="Ten";break;                       case 11:onez="Eleven";break;                       case 12:onez="Twelve";break;                       case 13:onez="Thirteen";break;                       case 14:onez="Fourteen";break;                       case 15:onez="Fifteen";break;                       case 16:onez="Sixteen";break;                       case 17:onez="Seventeen";break;                       case 18:onez="Eighteen";break;                       case 19:onez="Nineteen";break;```
to "one's" switch statement. But still does not output the correct answer.

My formula is right, isn't it?
Code:

`ans=thousandz+hundredz+tenz+onez;`
What could be the wrong in these codes?

I am not supposed to use arrays here. I'm just starting. I've not reached arrays yet. :(
• 01-01-2010, 07:32 AM
Eranga
Quote:

Originally Posted by MyOnlineChurva
I thought my logic was right. I don't get what's wrong with it. I've already transferred this:

Please take a sheet of paper and a pencil. Then take the user input as 12, and write down the output in each step in your code.

If you are confusing with arrays leave it at the moment. But keep in mind as Fubarable says arrays is the most efficient way to do this.

Let us know what's your logic gives.
• 01-01-2010, 02:21 PM
Supamagier
Do you like your coding style? :S
• 01-01-2010, 02:46 PM
Eranga
Quote:

Originally Posted by Supamagier
Do you like your coding style? :S

That's a very good point my friend. But I'm not sure even our OP think about that when he writing this code. :D
• 01-02-2010, 02:07 AM
MyOnlineChurva
Hi, sir. I added some codes to my program. It displays correct now. 17 is now Seventeen, 34 is now Thirty Four.

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 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". The tens in the 4-digit number is not displaying. :(
• 01-02-2010, 04:24 AM
Eranga
For me, the logic you follows is Okay. But your coding style is not good, not efficient as well. It's hard to read. You know that most of developers use the concept called dry run, means that run the code in his/her mind without actually running/debugging. It's easy to track logic errors most of the time, not 100% accuracy. Keep in mind that tracking logical errors is not an easy task.

As Fubarable says, think about the implementation of arrays in your code.
• 01-02-2010, 08:21 AM
RajShri
HI All,

I am new to this forum and java also.
I think the problem is in ur tens calculation.
try the following:

int tens=Math.round((num%100)/10);

sorry for my poor english

Bye
• 01-07-2010, 12:16 PM
MyOnlineChurva
@Eranga & Fubarable

The program is running now, very well. My problem now is how it's going to accept negative numbers.

Code:

```              if (in<0) {                   System.out.println("Your input is "+ans);               }```
won't work.

I don't really know how to do this, honestly. I mean what can I do to make this program accept negative numbers.

To be something like this (input: -9861)
Code:

`Your input is "Negative Nine Thousand Eight Hundred Sixty One"`
By the way

Thank you for your advice..As I've said I'm just starting to use Java Language. Arrays is the next topic I'll study. But for now I'll stick to these codes first. Your advices are noted. Thank you very much!
• 01-07-2010, 07:49 PM
Supamagier
Code:

```boolean negative = num < 0; num = Math.abs(num); // code (that finds the number in words) if (negative)     ans = "Negative"+numberInWords; else     ans = numberInWords;```
That's one easy way to to it.
• 01-07-2010, 07:55 PM
JosAH
Quote:

Originally Posted by MyOnlineChurva
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;```
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". The tens in the 4-digit number is not displaying. :(

Check your initial calculations by hand; pay special attention how you calculate your 'tens' value.

kind regards,

Jos
• 01-07-2010, 07:59 PM
Supamagier
Quote:

Originally Posted by Eranga
That's a very good point my friend. But I'm not sure even our OP think about that when he writing this code. :D

Maybe his 'space button' is very pointy. :rolleyes:
• 01-07-2010, 10:42 PM
MyOnlineChurva
Quote:

Originally Posted by JosAH
Check your initial calculations by hand; pay special attention how you calculate your 'tens' value.

kind regards,

Jos

I'm already done with calculating the values. The program runs very well now, accepting positive numbers from 1-9999, displaying the right output. Thanks anyway. :)

Quote:

Originally Posted by Supamagier
Code:

```boolean negative = num < 0; num = Math.abs(num); // code (that finds the number in words) if (negative)     ans = "Negative"+numberInWords; else     ans = numberInWords;```
That's one easy way to to it.

What's 'Math.abs'? I would take newbie codes first, not advanced. So that I can easily understand. Please.

Quote:

Originally Posted by Supamagier
Maybe his 'space button' is very pointy. :rolleyes:

Thanks. :)
• 01-07-2010, 11:09 PM
MyOnlineChurva
Quote:

Originally Posted by Supamagier
Code:

```boolean negative = num < 0; num = Math.abs(num); // code (that finds the number in words) if (negative)     ans = "Negative"+numberInWords; else     ans = numberInWords;```
That's one easy way to to it.

Just tested it now, it won't display the +ans.

It's just the "Your input is Negative" //if the number input is negative

this is how i did it:

Code:

```              boolean negative = num < 0;               num = Math.abs(num);                             if (num>=1&&num<=9999) {               //... the switches```
Code:

```        //after the switches                   if (negative)                         ans = "Negative "+thousandz+hundredz+tenz+onez;                                     else                         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();```
• 01-08-2010, 02:41 AM
Supamagier
That's exactly what I meant. ;)

Math.abs takes the absolute of the inputted number. This means that it will always be positive.
Quote:

-10 will become 10
8 will stay 8
-2 will become 2
etc