Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help: Num to words

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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"

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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help: Num to words

    Mmm... I had code to do just this somewhere

    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.

    switch(tenz)
    {
         case 1:
              switch (onez)
              {
                        case 0:
                                  // "ten"
                        case 1:
                                  // "eleven"
                        //... etc etc
              }
         case 2:
              // ... etc etc
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    shamed (January 6th, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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"?..

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

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    tens = (num/10) %10;

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    shamed (January 6th, 2010)

  7. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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:
    ((in>=1&&in<=9999) || (in>=-9999||in<=1))

    but it does not output correct again

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help: Num to words

    I would check the negative part first:

    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

  9. #7
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Help: Num to words

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

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

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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

Similar Threads

  1. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 04:28 AM
  2. Adding Marathi words to MySQL table
    By vaishali in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 8th, 2009, 06:43 AM