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

Thread: Convert Numbers from -999 to 999 in words!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Convert Numbers from -999 to 999 in words!

    Hi, I need help with my program written in DrJava. I'm very new to Java and need to write a program where the user inputs any number from -999 to 999 and it inputs it in words. For ex -24 entered would print negative twenty four or 800 entered would be eight hundred. I need to have at least 2 methods that need to be returned to the main method. I have some ideas so far but need help. When i compile, it says that cannot convert from void to java.lang.string for all the word = ... in my case statements. Help me! Please and thank you very much. I appreciate you thoughts and advice!

    import java.util.Scanner;
     
    public class NumToText {
      public static void main (String args[]){  
     
        Scanner input = new Scanner (System.in) ;
        System.out.println ("Enter number.");
        int number = input.nextInt ();
     
     
        if (number > 99 && number < 1000) {
                int h = number / 100;    //find the hundreds only, next step to print tens and ones
                hundreds(h);             //print number
     
                // test if they are from 11 to 19 called teens
     
                int x = 0;               // initialized variable x for calculations
                x = number % 100;        // find remainder of hundreds like 51
     
                if (x > 10 && x < 20) {  // ex,is 51 a teens number?
                    teens (x);          // print number
                }
                // if not teens number -> split up into tens & units
                if (x > 0 && x < 100) {
     
                    int tens = x / 10;   // find the tens 
                    tens(tens);          // print number
     
                    int ones = x % 10;  // finding the units (one, two)
                    ones(ones);        // print number
                }
            }
     
     
              } 
     
      static String ones (int number) {
     
        String word = "";
     
        switch(number) {
          case 0: 
            word= System.out.print("zero"); break;                                                             
          case 1: 
            word= System.out.print("one"); break;
          case 2: 
            word= System.out.print("two"); break;
          case 3:
            word= System.out.print("three"); break;
          case 4: 
            word= System.out.print("four"); break;
          case 5: 
            word= System.out.print("five"); break;
          case 6: 
            word= System.out.print("six"); break;
          case 7: 
            word= System.out.print("seven"); break;
          case 8: 
            word= System.out.print("eight"); break;
          case 9: 
            word= System.out.print("nine"); break;
        }      
        return word;
      }
     
      static String teens (int number) {
     
        String word = "";
     
        switch(number) {
          case 11: 
            word= System.out.print("eleven"); break;
          case 12: 
            word= System.out.print("twelve"); break;
          case 13: 
            word= System.out.print("thirteen"); break;
          case 14:
            word= System.out.print("fourteen"); break;
          case 15: 
            word= System.out.print("fifteen"); break;
          case 16: 
            word= System.out.print("sixteen"); break;
          case 17: 
            word= System.out.print("seventeen"); break;
          case 18: 
            word= System.out.print("eighteen"); break;
          case 19: 
            word= System.out.print("nineteen"); break;
     
        }
        return word;
      }
     
      static String tens (int number) {
     
        String word = "";
     
        switch(number) {
          case 10: 
            word= System.out.print("ten"); break;
          case 20:  
            word= System.out.print("twenty"); break;
          case 30: 
            word= System.out.print("thirty"); break;
          case 40: 
            word= System.out.print("fourty"); break;
          case 50: 
            word= System.out.print("fifty"); break;
          case 60:
            word= System.out.print("sixty"); break;
          case 70: 
            word= System.out.print("seventy"); break;
          case 80: 
            word= System.out.print("eighty"); break;
          case 90: 
            word= System.out.print("ninety"); break;
     
        }    
        return word;
      }
     
      static String hundreds (int number) {
     
        String word = "";
     
        switch(number) {
          case 10: 
            word= System.out.print("one hundred"); break;
          case 2: 
            word= System.out.print("two hundred"); break;
          case 3:
            word= System.out.print("three hundred"); break;
          case 4: 
            word= System.out.print("four hundred"); break;
          case 5: 
            word= System.out.print("five hundred"); break;
          case 6: 
            word= System.out.print("six hundred"); break;
          case 7: 
            word= System.out.print("seven hundred"); break;
          case 8: 
            word= System.out.print("eight hundred"); break;
          case 9: 
            word= System.out.print("nine hundred"); break;
     
        }    
        return word;
      }
    }
    Last edited by loma; May 19th, 2014 at 03:19 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Convert Numbers from -999 to 999 in words!

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    You've identified the problems. These statements are not valid Java statements:

    word = System.out.print("one");

    Assigning an output statement to a String variable is . . . well, nonsense to the compiler and confusing to humans wondering what you're trying to do.

    In the above statement, I think you're trying to assign "one" to word, which would simply be:

    word = "one";

    Try converting your existing statements to the more simple format I've given you and then come back when you have more questions.

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

    loma (May 20th, 2014)

  4. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Convert Numbers from -999 to 999 in words!

    Is it necessary to use a switch statement? Actually you can use another algorithm which is better than that and an easy way of converting number figures to words. Ex:

    Assign an array of String whose values are one, two... nine.
    Ex:
    String[] ones = {"","one","two","three","four","five","six","seven ","eight","nine"};

    As you noticed, value of first element in array is empty, well the reason is we are not going to use that, we are going to use the indexes of element as the number figure equivalent of each number word.
    word "one" is at index 1
    word "two" is at index 2.

    so if I input 1 I'll just print it's value in String Array.
    public static void main(String[] args) {
         String[] ones = {"", "one", "two","three", "four", "five", "six", "seven", "eight", "nine"};
         int sampleInput = 2;
         System.out.println(ones[sampleInput]);
    }
    example code above will print "two" as element of ones array at index 2 has a value of "two"

    but what about the numbers compose of multiple digits? just create another array of each unit
    Ex:
    String[] ones = {"","one","two","three","four","five","six","seven ","eight","nine"};
    String[] tens = {"", "", "twenty", "thirty","forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    as you notice, there is not "ten" in tens array, It's because I believed that "ten" belongs to special numbers like 11,12-19

    so how are you going to print 24?
    here it is:
    just get the first digit which is 2
    then print the element in array tens using index 2:
    tens[2] = twenty
    then print space
    then get the one's digit and print the element in array ones:
    ones[4] = four.

    that would make an output of twenty four

    how about three digits?
    just create another array let say units
    String[] units = {"", "thousand", "million", "billion"}; // you can expand it even to fillions
    I did not put "hundred" in it. (I don't know how to explain, I just don't feel to put hundred)

    how are we going to print 891?
    here it is
    get the first digit 8
    then print ones[8] = eight
    then print space
    then print "hundred" print space
    next get the second digit 9
    then print tens[9] = ninety
    then print space
    then get the last digit 1
    then print ones[1]
    that will make an output of eight hundred ninety one


    and actually there is a pattern in converting number figures to words:
    999 999 999 999 is equal to:
    nine hundred ninety nine billion
    nine hundred ninety nine million
    nine hundred ninety nine thousand
    nine hundred ninety nine

    you just have to be creative in coding.

  5. The Following User Says Thank You to dicdic For This Useful Post:

    loma (May 20th, 2014)

  6. #4
    Junior Member
    Join Date
    May 2014
    Posts
    5
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Convert Numbers from -999 to 999 in words!

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    You've identified the problems.
    Okay. I have figured it out somewhat. I have got it to print from 0 to 999 by taking your advice. Now i'm just wondering how to make it do the same program but with negative numbers. Below is the updated part of my code where its in the main method and is right below the Scanner code lines and before my switch cases. Nothing else has changed. When i add the if (number < 0) part, it shows negative in the output but doesnt show the rest of the number like 123. It just says negative. And not the 123 because it doesn't check the other ifs because i said like for example > 99 and < 1000 so it doesn't check the negatives. Thank you again.
    if (number == 0){
          System.out.print ("zero");
        }
     
        if (number < 0){
          System.out.print ("negative" );
        }
     
     
        if (number > 99 && number < 1000) {
          int h = number / 100;                        //find the hundreds only, next step to print tens and ones
          System.out.print (hundreds(h)+ " and ");                
        }
     
          int x = 0;                                  // initialized variable x for calculations
          x = number % 100;        
     
     
        if (x > 0 && x < 100) {
     
          if (x > 10 && x < 20) {  
          System.out.print (teens (x));          
          } else {
     
          int tens = x / 10;                    // find the tens 
          System.out.print(tens(tens)+ " ");         
     
          int ones = x % 10;                     // finding the ones
          System.out.print (ones(ones));        
          }
        }
      }

  7. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Convert Numbers from -999 to 999 in words!

    can you paste your whole code?

    --- Update ---

    Ok, I think I figured it out.
    see this conditions in your code:
    if (number > 99 && number < 1000)
    if (x > 0 && x < 100)


    those conditions above will never satisfy if the number is less than zero meaning negative.

    now look at this statement in your code:
    if (number < 0){
    System.out.print ("negative" );
    }


    you better convert number to positive value or its absolute value when you satisfy that condition.
    how? just multiply it by -1.

  8. The Following User Says Thank You to dicdic For This Useful Post:

    loma (May 25th, 2014)

  9. #6
    Junior Member
    Join Date
    May 2014
    Posts
    5
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Convert Numbers from -999 to 999 in words!

    Quote Originally Posted by dicdic View Post
    can you paste your whole code?

    --- Update ---

    Ok, I think I figured it out.
    see this conditions in your code:
    if (number > 99 && number < 1000)
    if (x > 0 && x < 100)


    those conditions above will never satisfy if the number is less than zero meaning negative.

    now look at this statement in your code:
    if (number < 0){
    System.out.print ("negative" );
    }


    you better convert number to positive value or its absolute value when you satisfy that condition.
    how? just multiply it by -1.
    i just solved it an hour after i posted my reply by multiplying it by negative one.

    thank you!

Similar Threads

  1. Problem with converting Numbers to Words! Kindly ask for help:(
    By ProgrammablePeter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 22nd, 2013, 10:26 AM
  2. convert numbers to text?
    By Sinead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2013, 12:44 PM
  3. Replies: 6
    Last Post: October 26th, 2013, 01:59 PM
  4. How to convert a digit to words
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 6th, 2013, 01:19 PM
  5. Replies: 4
    Last Post: November 14th, 2010, 05:02 PM