Results 1 to 13 of 13
Like Tree3Likes
  • 1 Post By mine0926
  • 1 Post By mine0926
  • 1 Post By JosAH

Thread: Programming with objects to turn a long number into strings.

  1. #1
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default Programming with objects to turn a long number into strings.

    Hello again everyone, so I have been working on this program for a couple of days, and I have screwed it up badly, re-fixed it, and screwed it all up again many many times. Here are the parameters for the assignment:

    I must use objects in java to take in an array of long type values from 0 to 999999999999L, and then convert them to String type without using parsing. Also, we are supposed to make the code as efficient as possible, creating methods for anything that should be a method. Here is my code thus far:

    Java Code:
    public class ConvertNumber1{
    	public static void main(String[] args) {
    
    		long[] testData = {0, 1, 7, 12, 20, 35, 50, 99, 100, 123, 300, 501, 700, 999,
    		                           1000,          1001,          1234,          9999,
    		                          10000,         11001,         12345,         99999,
    		                         100000L,       101001L,       123456L,       999999L,
    		                        1000000L,      1001001L,      1234567L,      9999999L,
    		                       10000000L,     11001001L,     12345678L,     99999999L,
    		                      100000000L,    101001001L,    123456789L,    999999999L,
    		                     1000000000L,   1001001001L,   1234567891L,   9999999999L,
    		                    10000000000L,  11001001001L,  12345678912L,  99999999999L,
    		                   100000000000L, 101001001001L, 123456789123L, 999999999999L};
    
    
    		for (int i=0; i<testData.length; i++) {
    			System.out.println(testData[i] + " is " + ConvertNumber.words(testData[i]));
    		}
    	}
    }
    
    
    
    class ConvertNumber {
    
      // A bunch of arrays that store all the different possible number names as Strings for later access
    	private static final String[] onesNames = {
    	    "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ",
    	    "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ",
    	    "seventeen ", "eighteen ", "nineteen "};
    	private static final String[] tensNames = {
    	    "", "ten ", "twenty ", "thirty ", "forty ", "fifty ",
    	    "sixty ", "seventy ", "eighty ", "ninety "};
    	private static final String[] hundredsNames = {
    			"hundred " , "one hundred " , "two hundred " , "three hundred " , "four hundred " ,"five hundred " , "six hundred ",
    			"seven hundred "  , "eight hundred " , "nine hundred "};
    
    	private static final String[] thousandsNames = {
    		"thousand " , "one thousand " , "two thousand " , "three thousand " , "four thousand " , "five thousand " , "six thousand ",
    		"seven thousand" , "eight thousand" , "nine thousand"};
    
    	private static final String[] millionsNames = {
    		"million " , "one million " , "two million " , "three million ", "four million " , "five million ",  "six million ",
    		"seven million ", "eight million " , "nine million "};
    
    	private static final String[] billionsNames = {
    		"billion " , "one billion " , "two billion " , "three billion " , "four billion " , "five billion " , "six billion ",
    	"seven billion " , "eight billion " , "nine billion "};
    
    
    	static String words(long number) {
    
    		long billions1;
    		long millions1;
    		long thousands1;
    		long hundreds1;
    		long billions2;
    		long millions2;
    		long thousands2;
    		long hundreds2;
    		String identifier = null;
    		String words;
    		String appendage = "";
    
    // this part determines the "chunks" of numbers. Dividing into billions, millions, thousands, and hundreds.  4 sets of 3 numbers is possible.
    		billions1 = (number / 1000000000L);
    		millions1= (number % 1000000000L);
    		thousands1 = (number  % 1000000);
    		hundreds1 = (number % 1000);
    
    //this part is to determine which decimal place the number reaches (i.e. millions, billions)  .  Used in the following if loop
    		billions2 = (number / 1000000000L);
    		millions2= ((number % 1000000000L) / 1000);
    		thousands2 = ((number  % 1000000) / 1000);
    		hundreds2 = (number % 1000);
    
    // Here I am assigning the identifier, so that later when the loop runs, it will know what type of number it just received. 
    		if (billions2 == 0) {
    
    			if (millions2 == 0) {
    
    
    				if (thousands2 == 0) {
    
    					identifier = "hundreds";
    				}
    
    				else {
    					identifier = "thousands";
    
    				}
    			}
    
    
    			else {
    				identifier = "millions";
    
    			}
    
    		}
    		else {
    			identifier = "billions";
    		}
    
    
    // This section creates the appendage that will be added at the end 
    //of every 3 digit section : 124,123,123 would be One hundred twenty
    //four MILLION, one hundred twenty three THOUSAND, one hundred twenty three 
    
    			if (identifier == "billions") {
    				appendage = "billion";
    			}
    			else if (identifier == "millions") {
    				appendage = "million";
    			}
    			else if (identifier == "thousands") {
    				appendage = "thousand";
    			}
    			else {
    				appendage = "";
    			}
    
    /* here is the loop to retrieve the strings from the array.  If I could figure out how to make this it's 
    own separate function without accessing the array directly I could just re-use the function on every  
    section of imported numbers, but I can't figure it out.  It won't let me access the array directly if I 
    make it a static function, and it wont let me use the variables from the other function if I make it a public function. 
    I'm so confused at this point!!! */
    	   					if (number == 0) {
    							words = "zero";
    						}
    						else if (number>0 && number<=19) {
    							// To be able to use "number" as index we need to convert it into "int" type
    							words = (onesNames[(int)number] + appendage);
    						}
    						else if (number > 19 && number <= 99) {
    
    							words = ((tensNames[((int)number/10)]) + onesNames[((int)number % 10)] + appendage);
    						}
    						else if (number > 99 && number <= 999) {
    
    							words = (hundredsNames[((int)number/100)] + tensNames[((int)number % 100)/10] + onesNames[((int)number % 10)] + appendage);
    
    						}
    						else {
    							words = "too big!";
    						}
    
    						return words;
    
    
    	}
    }
    I have it so that it interprets up to the hundreds place, and I understand it should be easy to loop it over and over again for the various decimal places, but I'm so confused and stuck. Any helping words?
    Last edited by broo7198; 10-07-2011 at 03:00 AM.

  2. #2
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default Re: Programming with objects to turn a long number into strings.

    two hours of tinkering later and I'm right back where I started!!! I'm getting so frustrated.. anyone out there that can help at all?

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Programming with objects to turn a long number into strings.

    Are you trying to convert numbers to words? Seems you manually interpreting each value from 0 to maximum number that you set.

    Do you have plan on how to do this?

  4. #4
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default Re: Programming with objects to turn a long number into strings.

    Yes, I am trying to convert numbers to words. I cannot using parsing of any kind. I'm not setting the numbers, the array is in the main, and I cannot modify the main in any way. I have it set up now so that it will take numbers up to 999 and interpret them. I also have a section that takes the number from the array, and splits it up into Billions, Millions, Thousands, and Hundreds. So I want to take the code that interprets up to 999, and re-use it on each section of the number. So if I get a number like 999,223,493,999. It will break it in to 999 , 223, 493, and 999. Then I can use the interpreter to convert those sections to words. Another problem I am running into is identifying whether the section is Billions, Millions, Thousands, or hundreds. That's where I'm at :/

  5. #5
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default Re: Programming with objects to turn a long number into strings.

    I am also very inexperienced with java, so if you see that I am doing something an inefficient way or a stupid way, you should tell me!

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Programming with objects to turn a long number into strings.

    Its good start that you know that you can split numbers into something like "999, 999, 999.00".
    But before doing that, I think the first thing is to convert single numbers. So write a code that convert numbers "985" into "nine eight five".

  7. #7
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default Re: Programming with objects to turn a long number into strings.

    the code I have up there will already do that. If u compile and run it translates all the way up to 999. I need to figure out how to implement that on to larger numbers.

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Programming with objects to turn a long number into strings.

    Do not compare strings like
    Java Code:
    identifier == "billions"
    instead
    Java Code:
    "billions".equals(identifier)
    * Use DecimalFormat to format numbers into "123,456,789"
    * then split this numbers using comma(","), an array of string will hold this
    * then use a for loop to convert each array. Inside the loop is your code that converts it into words.
    broo7198 likes this.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Programming with objects to turn a long number into strings.

    If I were to convert numbers in hundreds I will just use the ONES array and then just add where word "hundred " after the word in ONES.
    To do this I will have a method in which has a parameter of a digit and its place

    This is just a sample, just to explain what I am trying say
    Java Code:
        private String onesAndHundreds(int aSingleNumber, int place)
        {
            String word = "";
            word = ONES[aSingleNumber];
            if(place == 3)
            {
                word += "HUNDRED ";
            }
            return word;
        }
    And determining thousand, million and so on will be in separated method.
    Last edited by mine0926; 10-07-2011 at 05:29 AM. Reason: change method name
    broo7198 likes this.

  10. #10
    broo7198 is offline Member
    Join Date
    Aug 2011
    Location
    West Virginia
    Posts
    38
    Rep Power
    0

    Default Re: Programming with objects to turn a long number into strings.

    thanks a lot, this definitely helps!

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Programming with objects to turn a long number into strings.

    Maybe this old thread may help as well.

    kind regards,

    Jos
    mine0926 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Programming with objects to turn a long number into strings.

    @JosAh, your code (post#5) in the linked thread is simpler and cleaner. I have done numbers to words before but its a long method.
    It format the number into #,##0.00, split it using comma, and loop. Would have never thought that way of coding, I will change my code then. Learned something again
    :)

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Programming with objects to turn a long number into strings.

    Quote Originally Posted by mine0926 View Post
    @JosAh, your code (post#5) in the linked thread is simpler and cleaner. I have done numbers to words before but its a long method.
    It format the number into #,##0.00, split it using comma, and loop. Would have never thought that way of coding, I will change my code then. Learned something again
    :)
    I tried to do the same for a couple of other languages but failed miserably; e.g the French don't write seventy, eighty, ninety, but write sixty-ten, four-twenty, four-twenty-ten and the Dutch reverse the words for the units and tens, e.g. they say nine-and-eighty for eighty-nine and there are more messy irregularities. English is easy and quite regular compared to those languages ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Number Format Exception while parsing long
    By Aamir in forum Networking
    Replies: 2
    Last Post: 05-15-2011, 02:57 AM
  2. Find the index number of strings in an arraylist
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 03:25 PM
  3. Handling long strings - Advice needed
    By Codercode in forum Forum Lobby
    Replies: 0
    Last Post: 01-02-2011, 12:43 AM
  4. Replies: 7
    Last Post: 09-28-2010, 12:17 AM
  5. Objects programming?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 07-08-2009, 06: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
  •