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: need help converting numbers to word

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default need help converting numbers to word

    guys, I wrote this program that converts numbers to words eq: 4914 to "four thousand and nine hundred and fourteen"; however, I'm having problems with the toString method. Is not printing correctly some numbers. the functions and their return type can't be changed. I need help with the toString method that actually puts all the words together. I appreciate any help.

    package numberstowords;
     
    import java.util.*;
     
    public class Numbers {
     
        //array containing single digits words numbers:0-9
        private final String[] SINGLE_DIGITS_WORDS;
     
        //array containing special words words eg:10-19
        private final String[] TEEN_DIGITS_WORDS;
     
        //array containing tens words numbers:20-90
        private final String[] TEN_DIGITS_WORDS;
     
        private int value,   //number to be converted to words
                    one,     //number to store digits
                    ten,     //number to store tens
                    hundred, //number to store hundred
                    thousand;//number to store thousand
     
        private String strOut;
     
        //getting single digit number
        private int getOnes()
        {
            one = value % 10;
            return one;
        }
     
        //getting tens numbers
        private int getTens()
        {
            ten = value % 100;
            ten /= 10;
            return ten;
        }
     
        //getting hundreds numbers
        private int getHundreds()
        {
            hundred = value % 1000;
            hundred /= 100;
            return hundred;
        }
     
        //getting thousands numbers
        private int getThousands()
        {
            thousand = value % 10000;
            thousand /= 1000;
            return thousand;
        }
     
        //converting and returning string of ones 
        private String digitsToString(int one)
        {
            return SINGLE_DIGITS_WORDS[one];        
        }
     
        //converting and returning strings of tens and teens
        private String tensAndOnesToString(int ten, int one)
        {
            if(ten == 1)//if number is a teen return, else return tens 
            { 
                return TEEN_DIGITS_WORDS[one];
            }
            return TEN_DIGITS_WORDS[ten-2];         
        }
     
        //converting and returning strings of hundreds
        private String hundredsToString(int hundred)
        {
            return digitsToString(hundred) + " hundred";
        }
     
        private String thousandsToString(int thousand)
        {
            return digitsToString(thousand) + " thousand";
        }
     
        @Override
        public String toString()
        {
     
      }

    Tester
    package numberstowords;
     
    import java.util.Scanner;
     
    public class Test {
     
        //array to store the word equivalent of numbers
        public static String[] s = new String[8];
     
        public static void main(String[] args) {
     
            int count = 0;    
            Scanner input = new Scanner(System.in);
            do
            {
                System.out.println("Enter an integer (4 digits max)--> ");
                int value = input.nextInt();
                if(value > 0)
                {
                    Numbers n = new Numbers(value);
                    //store n.toString in an arrayList s
                    //s[count] = n.toString();
                    System.out.println(n.toString());
                    count++;
                }
                else break;
            }while(true);
    Last edited by mia_tech; May 15th, 2012 at 10:09 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: need help converting numbers to word

    Give us some test cases so we can understand what is happening. Tell us example inputs, what your program outputs, and what it should be outputting. That will help everyone narrow down the problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help converting numbers to word

    Quote Originally Posted by aussiemcgr View Post
    Give us some test cases so we can understand what is happening. Tell us example inputs, what your program outputs, and what it should be outputting. That will help everyone narrow down the problem.
    Well, the program convert numbers from 1 to 9999 into words , I'm almost done with the exception of two numbers that I'm having problem printing... look at the bottom of post for errors.
    By the way I modified the Numbers class and now the it looks like this:

    package numberstowords;
     
    import java.util.*;
     
    public class Numbers {
     
     
        //getting single digit number
        private int getOnes()
        {
            int n1, n2;
            n1 = value / 10;
            n2 = n1 * 10;
            one = value - n2;  
            value = n1;
            return one;
        }
     
        private int getTens()
        {
            int n1, n2;
            n1 = value / 10;
            n2 = n1 * 10;
            ten = value - n2;
            value = n1;
            return ten;
        }
     
        private int getHundreds()
        {
            int n1, n2;
            n1 = value / 10;
            n2 = n1 * 10;
            hundred = value - n2;
            value = n1;
            return hundred;
        }
     
        private int getThousands()
        {
            int n1, n2;
            n1 = value / 10;
            n2 = n1 * 10;
            thousand = value - n2;
            value = n1;
            return thousand;
        }
     
        //converting and returning string of ones 
        private String digitsToString(int one)
        {
            return SINGLE_DIGITS_WORDS[one];        
        }
     
        //converting and returning strings of tens and teens
        private String tensAndOnesToString(int ten, int one)
        {
            if(ten == 1)//if number is a teen return, else return tens 
            { 
                return TEEN_DIGITS_WORDS[one];
            }
            return TEN_DIGITS_WORDS[ten];
     
        }
     
        //converting and returning strings of hundreds
        private String hundredsToString(int hundred)
        {
            return digitsToString(hundred) + " hundred";
        }
     
        private String thousandsToString(int thousand)
        {
            return digitsToString(thousand) + " thousand";
        }
     
     
        }
      }
    Enter an integer (4 digits max)--> 
    5101
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
    	at tester.Tester.tensAndOnesToString(Tester.java:75)
    	at tester.Tester.toString(Tester.java:124)
    	at tester.Tester.main(Tester.java:144)
    Java Result: 1

    note how is giving an out of bound exception -2. That is signal that I'm accessing an array with a non existing index, but I can't see where. only happens when I print a four digit number that contains a 0 in the third digit

    and

    Enter an integer (4 digits max)--> 
    7376
    Seven thousand and three hundred and seventysix
    Enter an integer (4 digits max)-->

    in this one notice the last number "seventysix" that doesn't happen if I print 76 or 376, only happens in the thousands like 5476. it happens when I print thousands, it joins together the last two numbers

    Thanks for the help
    Last edited by mia_tech; May 15th, 2012 at 10:09 AM.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: need help converting numbers to word

    Hello mia_tech!
    I tested your code from post#1 and it seems to work as expected. I also tried the numbers that cause you error or bug (5101 and 7376) and they worked fine for me.
    What was wrong with the first posted code? Can you give us the modifications between the code on post#1 and post#3?

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help converting numbers to word

    Quote Originally Posted by andreas90 View Post
    Hello mia_tech!
    I tested your code from post#1 and it seems to work as expected. I also tried the numbers that cause you error or bug (5101 and 7376) and they worked fine for me.
    What was wrong with the first posted code? Can you give us the modifications between the code on post#1 and post#3?
    there's a small modification in the toString() method, look where is says if(d3 != 0). The 1st code was not printing teen numbers correctly, it was printing "fifteenfive" for 15. I fixed that in the 2nd code; however, a new problem came up which I explained in my previous post

    Thanks

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: need help converting numbers to word

    Quote Originally Posted by mia_tech View Post
    there's a small modification in the toString() method, look where is says if(d3 != 0). The 1st code was not printing teen numbers correctly, it was printing "fifteenfive" for 15. I fixed that in the 2nd code; however, a new problem came up which I explained in my previous post

    Thanks
    As I told you in my last post I've tried your code several times with many testing numbers and I don't get the problems you posted. But the problem with "fifteenfive" for 15 is still happening if a three or four digit number is input.
    (ie input ---> 3415
    output ---> Three thousand and four hundred and fifteen five)
    Notice that "fifteen five" is separated with a space for me (not fifteenfive)

Similar Threads

  1. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  2. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  3. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  4. Word Search Help
    By Tanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2011, 01:39 AM
  5. Replies: 4
    Last Post: November 14th, 2010, 05:02 PM