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

Thread: Number in words java proram

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Number in words java proram

    Hello i have to create a program witch given a int number random has to convert it in words for example int number is 4 it should return "four", should return the number as series of words (all lowercase). So far i have written this(but it seems it is not working ):

    public static final String[] units = { "", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
    "Twelve","Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen" };

    public static final String[] tens = { "","","Twenty","Thirty","Forty", "Fifty","Sixty","Seventy","Eighty","Ninety"};

    String numberInWords(int number){
    if (number < 20) {return units[number];}
    if (number < 100) {return tens[number / 10] + ((number % 10 != 0) ? " " : "") + units[number % 10];}
    if (number < 1000) {return units[number / 100] + " Hundred" + ((number % 100 != 0) ? " " : "")
    + numberInWords(number % 100);}

    return numberInWords(number);

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Number in words java proram

    Where are your main method?
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Number in words java proram

    What is the error that you get when you compile/run the code?

  4. #4
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    Posts
    27
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Smile Re: Number in words java proram

    Dear Bogdan321:

    Hello, and a pleasant day.

    Without using arrays, I strongly suggest the power of division (/) and modulus division ( % ) of the number you want to convert into words. For example, you declare something like:

    int number = 987;


    987 composes of three number places: hundreds place (9), the tens place (8), and the ones place (7).

    Now we need to "extract" the hundred place (9) by doing this arithmetic operation:

    int h;
    h = number / 100;

    This will give the value of 9 to our variable h. Then using a simple switch/case, we could do some thing like:

    switch (h) {
       case 1: "one hundred "; break;
       case 2: "two hundred "; break;
    }

    Just add more case statement until you reach case 9 : "nine hundred "; break;

    Then we only have 87. So to "extract" 87 from 987, we could use modulus division:

    int hrem;
    hrem = number % 100;
    //add "special if/else" condition if hrem is 11,12,13,14,15,16,17,18 or 19

    Now, the variable hrem is equal to 87. Now we need to "extract" the tens place (8) by doing this arithmetic operation:

    int t;
    t = hrem / 10;

    This will give the value of 8 to our variable t. Then using a simple switch/case, we could do something like:

    switch (t) {
       case 1: "ten "; break;
       case 2: "twenty "; break;
    }

    Just add more case statement until you reach case 9 : "ninety "; break;

    Then last, you need the ones place (7). Thus:

    int o;
    o = hrem % 10;

    Then using a simple switch/case, we could do some thing like:

    switch (o) {
       case 1: "one "; break;
       case 2: "two "; break;
    }

    Again, just add more case statement until you reach case 9 : "nine "; break;


    NOTE: Please take note that you need a "special" if/else condition if the variable hrem is between 11 to 19. You may notice on my comments above.

    Hope this will help you.


    Thank you and God bless us all.


    Respectfully Yours,

    Mark Squall
    Last edited by marksquall; April 30th, 2018 at 04:33 AM. Reason: Wrong spelling detected.

Similar Threads

  1. How to print the number of palindrome words in a sentence?
    By CupCake41 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2017, 02:57 PM
  2. CONVERT NUMBER TO WORDS USING IF ELSE CODES
    By rojo_csc101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 23rd, 2014, 08:03 AM
  3. Delimiter function and number of words in string
    By Java girl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 22nd, 2014, 05:08 PM
  4. Replies: 2
    Last Post: September 29th, 2012, 07:42 PM
  5. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 04:28 AM