Results 1 to 17 of 17
  1. #1
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default 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 {
            
            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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    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. :(

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by MyOnlineChurva View Post
    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.

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Do you like your coding style? :S
    I die a little on the inside...
    Every time I get shot.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Supamagier View Post
    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

  8. #8
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  10. #10
    RajShri is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  11. #11
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    @Eranga & Fubarable

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

    Adding:
    Java 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)
    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!

  12. #12
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.
    I die a little on the inside...
    Every time I get shot.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by MyOnlineChurva View Post
    Java 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

  14. #14
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Wink

    Quote Originally Posted by Eranga View Post
    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:
    I die a little on the inside...
    Every time I get shot.

  15. #15
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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 View Post
    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.

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

  16. #16
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Supamagier View Post
    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("Your input is "+ans);
                       System.out.println("Continue?");
                       System.out.println("Press Y if YES, N if NO.");
                       o=input.readLine();

  17. #17
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. alphabatize words without using arryas???
    By echo33 in forum New To Java
    Replies: 5
    Last Post: 04-22-2009, 10:34 AM
  2. Jumble Multiple Words
    By wethekings in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 03:57 PM
  3. Money to Words
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-30-2008, 04:27 AM
  4. Scrambling Words
    By Shadow22202 in forum New To Java
    Replies: 9
    Last Post: 04-30-2008, 03:51 AM
  5. help w words
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 11-21-2007, 06:15 PM

Posting Permissions

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