# Thread: Help: Num to words

1. Member
Join Date
Dec 2009
Posts
10
Rep Power
0

## 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:
Java Code:
```import java.io.*;

public class NewClass {

public static void main(String[]args) throws IOException {

String o="Y";
String x;

do {String ans="",onez="",tenz="",hundredz="",thousandz="";

try {
System.out.println("Enter a number from 1 to 9999:");
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("Continue?");
System.out.println("Press Y if YES, N if NO.");
}

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.");;
}

}

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.

2. 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?

3. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
I thought my logic was right. I don't get what's wrong with it. I've already transferred this:
Java 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?
Java 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. :(

4. 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.

5. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
Do you like your coding style? :S

6. 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

7. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
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"?..

Java Code:
```try {
System.out.println("Enter a number from 1 to 9999:");
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("Continue?");
System.out.println("Press Y if YES, N if NO.");
}

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.");;
}```
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. :(

8. 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.

9. Member
Join Date
Jan 2010
Posts
3
Rep Power
0
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

10. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
@Eranga & Fubarable

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

Java Code:
```               if (in<0) {
}```
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)
Java 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!

11. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
Java 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.

12. Originally Posted by MyOnlineChurva
Java Code:
```try {
System.out.println("Enter a number from 1 to 9999:");
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

13. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
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:

14. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
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. :)

Originally Posted by Supamagier
Java 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.

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

15. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
Originally Posted by Supamagier
Java 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:

Java Code:
```               boolean negative = num < 0;
num = Math.abs(num);

if (num>=1&&num<=9999) {
//... the switches```
Java Code:
```         //after the switches
if (negative)
ans = "Negative "+thousandz+hundredz+tenz+onez;

else
ans = thousandz+hundredz+tenz+onez;

System.out.println("Continue?");
System.out.println("Press Y if YES, N if NO.");

16. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
That's exactly what I meant. ;)

Math.abs takes the absolute of the inputted number. This means that it will always be positive.
-10 will become 10
8 will stay 8
-2 will become 2
etc

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•