Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Turning numbers into words

  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Turning numbers into words

    Hello. Sorry in advance for the stupid and long java question.

    I want to write a java program that will turn any number of up to 9 digits in length into the correct englsh words for that number. So if the user inputs 1234, the program will respond with: one thousand two hundred and thirty four. Or if the user inputs 7231560, the program responds with: seven million, two hundred and thrity one thousand, five hundred and sixty. The caveats: the number can be of any length from 1 to 999999999, and doesn't include any commas or other delimiters.

    I have successfully written a program that will do this for any number of three digits in length. I then re-wrote the program (posted below) to write a special method to do this conversion of any three digits into the proper words. I was thinking that once I had this special method, it should be easy for me to apply the three digit method to larger numbers. BUT, to apply the three digit method to a larger number, I have to split up the larger number into groups of 3, but in reverse order (so 7231560 becomes 560, 231, and 7, and not 723, 156, and zero).

    SO - my question: how do I split up a number of unknown length into groups of 3 from back to front? Also - my approach of applying my three digit method to a larger number may not be the best approach to accomplish the overall task of turning a number into words. Is there a better way to do this?

    Here, the program I wrote that turns any three digit number into the proper english words.

    Java Code:
    public class SayNumber {
    	
    	public static String Say3(String s) {
    		String onePlace = "";
    		String tenPlace = "";
    		String hundredPlace = "";
    		String wordOut = "";
    		int[] tArray = new int[s.length()];
    		for (int i = 0; i < s.length(); i++) 
    			tArray[i] = Character.digit(s.charAt(i), 10);
     
    		String[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    		String[] teens = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    		String[] tens = { null, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    		
    		if (s.length() == 1){
    			onePlace = (ones[tArray[0]]);
    			wordOut = onePlace;
    		}
    		else if ( (s.length() == 2) && (tArray[1] == 0)) {
    			tenPlace = (tens[tArray[0]]);
    			wordOut = tenPlace;
    		}
    		else if ((s.length() == 2) && (tArray[0] == 1)) {
    			tenPlace = (teens[tArray[1]]);
    			wordOut = tenPlace;
    		}
    		else if ( (s.length() == 2) && (tArray[1] != 0) ) {
    			tenPlace = (tens[tArray[0]]);
    			onePlace = (ones[tArray[1]]);
    			wordOut = (tenPlace + "-" + onePlace);
    		}
    		else if ((s.length() == 3) && (tArray[1] == 0) && tArray[2] == 0) {
    			hundredPlace = (ones[tArray[0]]);
    		 	wordOut = (hundredPlace + " hundred");
    		}
    		else if ((s.length() == 3) && (tArray[1] > 1) && (tArray[2] == 0)) {
    			hundredPlace = (ones[tArray[0]]);
    			tenPlace = (tens[tArray[1]]);
    			wordOut = (hundredPlace + " hundred and " + tenPlace);
    		}
    		else if ((s.length() == 3) && (tArray[1] == 0)) {
    			hundredPlace = (ones[tArray[0]]);
    			onePlace = (ones[tArray[2]]);
    			wordOut = (hundredPlace + " hundred and " + onePlace);
    		}
    		else if ((s.length() == 3) && (tArray[1] == 1)) {
    			hundredPlace = (ones[tArray[0]]);
    			tenPlace = (teens[tArray[2]]);
    			wordOut = (hundredPlace + " hundred and " + tenPlace);
    		}
    		else if ((s.length() == 3) && (tArray[1] > 1)) {
    			hundredPlace = (ones[tArray[0]]);
    			tenPlace = (tens[tArray[1]]);
    			onePlace = (ones[tArray[2]]);
    			wordOut = (hundredPlace + " hundred and " + tenPlace + "-" + onePlace);
    		}
    		return wordOut; 
    	}
    
    	public static void main(String[] args) {
    		String target = args[0];
    					
    		if (target.length() < 4) 
    			System.out.print("The number is: " + Say3(target));
    		else
    			System.out.print("You Fool!!");
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    11

    Default Re: Turning numbers into words

    Take a look at the String API for useful methods that can give you things like a String's length, a substring of a String, etc.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Turning numbers into words

    Thank you Kevin for your useful suggestion. I will take a look.

    I think I might have solved the problem in a different fashion. It just occurred to me that if I have a number n, I can do n%1000 to give me the last three digits in their right order, no matter what the original length of n. Then (n/1000)%1000 will give me the next three digits again in proper order. I'm going to go play around with my program some more and see if I can make it work.

    Thanks again.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,154
    Rep Power
    9

    Default Re: Turning numbers into words

    Actually, n%1000 will give you any number between 0 and 999 depending on the value of n. So 10% of the possible remainders will have less than three digits.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,232
    Blog Entries
    7
    Rep Power
    24

    Default Re: Turning numbers into words

    Suppose you have a number bmmmtttuuu; if b isn't zero you pronounce it as b billiard etc. if mmm isn't zero you pronounce further as mmm million etc. the same goes for ttt thousand etc. all you have to do is write a method that can pronounce a number up to 999.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. changing telephone numbers into words.
    By akeni in forum New To Java
    Replies: 20
    Last Post: 11-24-2011, 01:37 PM
  2. Converting numbers to words
    By Zora in forum New To Java
    Replies: 2
    Last Post: 11-14-2011, 11:10 AM
  3. turning string numbers into doubles
    By imorio in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 11:25 PM
  4. Turning numbers into asterisks.
    By BugginVT in forum New To Java
    Replies: 7
    Last Post: 02-25-2009, 08:38 AM

Posting Permissions

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