Results 1 to 15 of 15
  1. #1
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Question Any Other Methods for Number to Word Conversion?

    I don't know exactly what this program is called but heres an example.

    number given: 248
    convert to word: two hundred forty eight


    Here's my code for that:

    Java Code:
    import static java.lang.System.out;
    
    public class NumberLetter {
    
        public static void main(String[] args) {
            int total = 151; //what if I want this to be user inputted? I can get a value like 1248
            int ones, tens, hundreds, thousands,left;
    
                thousands = total / 1000;
                left = total % 1000;
                
                hundreds = left / 100;
                left = left % 100;
    
                tens = left / 10;
                left = left % 10; //calculations to extract each number in each position
                
                ones = left; // ones will be the one left out on the calculation
    
                //this very long switch case is my problem
                //is there any other way around it?
                //I wasn't able to continue the switch because this is just for testing only.
                //what if I want like a thousand to a million? will I do this switch case over and over?
    
                switch (hundreds) {
                    case 1:
                        out.print("One hundred ");
                        break;
                    case 2: 
                        out.print("Two hundred ");
                        break;
                    case 3: 
                        out.print("Three hundred ");
                        break;
                    case 4: 
                        out.print("Four hundred ");
                        break;
                    case 5:
                        out.print("Five hundred ");
                        break;
                    case 6: 
                        out.print("Six hundred ");
                        break;
                    case 7: 
                        out.print("Seven hundred ");
                        break;
                    case 8:
                        out.println("Eight hundred ");
                        break;
                    case 9: 
                        out.println("Nine hundred ");
                        
                                
                } switch (tens) {
                    case 1: 
                        if (tens == 1 && ones == 0) {
                          out.println("ten");
                          break;
                        } else if (tens == 1 && ones == 1 ) {
                            out.println("eleven");
                            break;        
                        } if (tens == 1 && ones == 2) {
                          out.println("twelve");
                          break;
                        } else if (tens == 1 && ones == 3 ) {
                            out.println("thirteen");
                            break;        
                        } if (tens == 1 && ones == 4) {
                          out.println("fourteen");
                          break;
                        } else if (tens == 1 && ones == 5 ) {
                            out.println("fifteen");
                            break;        
                        } if (tens == 1 && ones == 6) {
                          out.println("sixteen");
                          break;
                        } else if (tens == 1 && ones == 7 ) {
                            out.println("seventeen");
                            break;        
                        } if (tens == 1 && ones == 8) {
                          out.println("eighteen");
                          break;
                        } else if (tens == 1 && ones == 9 ) {
                            out.println("nineteen");
                            break;        
                        } 
                    case 2: 
                        if (tens == 2 && ones == 0) {
                          out.println("twenty");
                          break;
                        } else if (tens == 2 && ones == 1 ) {
                            out.println("twenty one");
                            break;        
                        } if (tens == 2 && ones == 2) {
                          out.println("twenty two");
                          break;
                        } else if (tens == 2 && ones == 3 ) {
                            out.println("twenty three");
                            break;        
                        } if (tens == 2 && ones == 4) {
                          out.println("twenty four");
                          break;
                        } else if (tens == 2 && ones == 5 ) {
                            out.println("twenty five");
                            break;        
                        } if (tens == 2 && ones == 6) {
                          out.println("twenty six");
                          break;
                        } else if (tens == 2 && ones == 7 ) {
                            out.println("twenty seven");
                            break;        
                        } if (tens == 2 && ones == 8) {
                          out.println("twenty eight");
                          break;
                        } else if (tens == 2 && ones == 9 ) {
                            out.println("twenty nine");
                            break;        
                        } 
                    case 3: 
                        if (tens == 1 && ones == 0) {
                          out.println("thirty");
                          break;
                        } else if (tens == 3 && ones == 1 ) {
                            out.println("thirty one");
                            break;        
                        } if (tens == 3 && ones == 2) {
                          out.println("thirty two");
                          break;
                        } else if (tens == 3 && ones == 3 ) {
                            out.println("thirty three");
                            break;        
                        } if (tens == 3 && ones == 4) {
                          out.println("thirty four");
                          break;
                        } else if (tens == 3 && ones == 5 ) {
                            out.println("thirty five");
                            break;        
                        } if (tens == 3 && ones == 6) {
                          out.println("thirty six");
                          break;
                        } else if (tens == 3 && ones == 7 ) {
                            out.println("thirty seven");
                            break;        
                        } if (tens == 3 && ones == 8) {
                          out.println("thirty eight");
                          break;
                        } else if (tens == 3 && ones == 9 ) {
                            out.println("thirty nine");
                            break;        
                        } 
                    case 4: 
                        if (tens == 4 && ones == 0) {
                          out.println("fourty");
                          break;
                        } else if (tens == 4 && ones == 1 ) {
                            out.println("fourty one");
                            break;        
                        } if (tens == 4 && ones == 2) {
                          out.println("fourty two");
                          break;
                        } else if (tens == 4 && ones == 3 ) {
                            out.println("fourty three");
                            break;        
                        } if (tens == 4 && ones == 4) {
                          out.println("fourty four");
                          break;
                        } else if (tens == 4 && ones == 5 ) {
                            out.println("fourty five");
                            break;        
                        } if (tens == 4 && ones == 6) {
                          out.println("fourty six");
                          break;
                        } else if (tens == 4 && ones == 7 ) {
                            out.println("fourty seven");
                            break;        
                        } if (tens == 4 && ones == 8) {
                          out.println("fourty eight");
                          break;
                        } else if (tens == 4 && ones == 9 ) {
                            out.println("fourty nine");
                            break;        
                        } 
                    case 5: 
                        if (tens == 5 && ones == 0) {
                          out.println("fifty");
                          break;
                        } else if (tens == 5 && ones == 1 ) {
                            out.println("fifty one");
                            break;        
                        } if (tens == 5 && ones == 2) {
                          out.println("fifty two");
                          break;
                        } else if (tens == 5 && ones == 3 ) {
                            out.println("fifty three");
                            break;        
                        } if (tens == 5 && ones == 4) {
                          out.println("fifty four");
                          break;
                        } else if (tens == 5 && ones == 5 ) {
                            out.println("fifty five");
                            break;        
                        } if (tens == 5 && ones == 6) {
                          out.println("fifty six");
                          break;
                        } else if (tens == 5 && ones == 7 ) {
                            out.println("fifty seven");
                            break;        
                        } if (tens == 5 && ones == 8) {
                          out.println("fifty eight");
                          break;
                        } else if (tens == 5 && ones == 9 ) {
                            out.println("fifty nine");
                            break;        
                        } 
                }
           out.println("The number is " + total);
           
            }
            
            
    
          
          }
    So as you can see, it is a very long code. If it continues on to a thousand then this code will be four times longer.

    My question is:
    Are there any other method for this problem?
    What if I want it to expand it to a thousand or even a million? Will I go to the switch case over and over again?

    Any examples or suggestions will do as long as you don't solve the problem for me.

    Thank you.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Start by trying to improve the code that you have bit by bit.
    e.g Start by breaking it down into smaller methods that do a specific task. Instead of making it print to the console at each step, consider writing a method that takes the number and returns the textual representation.
    e.g Elementary improvements for hundreds can be
    : Instead of using that long case, write a method that takes an int between 0 and 10 and returns the words representation. Let's say the method is called 'words' you then simply replace the switch with
    Java Code:
    words(hundreds) + " hundreds"

  3. #3
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default Don't See the Picture yet

    Thanks for the advice. I am trying to work on it but I still aren't able to visualize the picture yet.

    Any other beginner advice?

    Thank you. ;)

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I don't know what you are stuck with now. Breaking down actions into smaller well defined ones is a good start. Also take advantages of arrays to shorten some parts of the code.

  5. #5
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default

    Do you happen to know enum? Is this possible here?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I've heard of enum, yes, but don't you want to follow my advice and work on improving the code that you have already written so far?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by arnelcolar View Post
    Thanks for the advice. I am trying to work on it but I still aren't able to visualize the picture yet.

    Any other beginner advice?

    Thank you. ;)
    Suppose you can do it for numbers in the range [0, 20). It is quite easy to construct bigger numbers up to 99 from it: simply prepend twenty, thirty, fourty ... ninety to the number n%10 and don't make it say "zero", e.g. 20 is written in words as "twenty", not at "twentyzero".

    For numbers less than 1000 it's also easy, just call the first method for the number n/100, add "hundred" and append the word produced by the second method for n%100.

    The same goes for numbers < 1,000,000. Take the result of the previous method for the number n/1000 add "thousand" and call the previous method again for the number n%1000.

    The same goes for numbers < 1,000,000,000 ... ints don't go much bigger than this.

    kind regards,

    Jos

  8. #8
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default

    I see. I'll update you on my progress later. Thanks.

  9. #9
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default

    Thank you r035198x, I'll do what you told me to. I'll update you on my progress later.

  10. #10
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    this will do it. ask anything u dont understand
    PHP Code:
    package chararray;
    
    import java.util.Scanner;
    
    public class Main {
            static String numberOut = "";
        public static void main(String[] args) {
    
            System.out.println("Enter number");
            Scanner sc = new Scanner(System.in);
            String numberTyped = sc.nextLine();
            char[] numberArray = numberTyped.toCharArray();
    
                getThouName(numberArray);
                getHunName(numberArray);
                getTenName(numberArray);
                getUnitName(numberArray);
    
           System.out.println(numberOut + "\n");
        }
        public static void getUnitName(char[] _numberArray){
    
            if(_numberArray.length > 1){  //prevent error, check length
                if(_numberArray[_numberArray.length - 2] == '1'){
                        switch(_numberArray[_numberArray.length - 1]){
                            case '1':
                                numberOut+=  " eleven";
                                break;
                            case '2':
                                numberOut+=  " twelve";
                                break;
                            case '3':
                                numberOut+=  " thirteen";
                                break;
                            case '4':
                                numberOut+=  " fourteen";
                                break;
                            case '5':
                                numberOut+=  " fifteen";
                                break;
                            case '6':
                                numberOut+=  " sixteen";
                                break;
                            case '7':
                                numberOut+=  " seventeen";
                                break;
                            case '8':
                                numberOut+=  " eighteen";
                                break;
                            case '9':
                                numberOut+=  " nineteen";
                                break;
                            default:
                                numberOut+=  "";
                        }
                }else{
                        singleWrite(_numberArray);
    
                }
            }else{
               singleWrite(_numberArray);
            }
    
        }
        public static void singleWrite(char[] _numberArray){
                      switch(_numberArray[_numberArray.length - 1]){
                            case '1':
                                numberOut+=  " one";
                                break;
                            case '2':
                                numberOut+=  " two";
                                break;
                            case '3':
                                numberOut+=  " three";
                                break;
                            case '4':
                                numberOut+=  " four";
                                break;
                            case '5':
                                numberOut+=  " five";
                                break;
                            case '6':
                                numberOut+=  " six";
                                break;
                            case '7':
                                numberOut+=  " seven";
                                break;
                            case '8':
                                numberOut+=  " eight";
                                break;
                            case '9':
                                numberOut+=  " nine";
                                break;
                            default:
                                numberOut+=  "";
                        }
        }
        public static void getTenName(char[] _numberArray){
                if(_numberArray.length > 1){  //prevent error, check length
                      switch(_numberArray[_numberArray.length - 2]){
    
                            case '2':
                                numberOut+= " twenty";
                                break;
                            case '3':
                                numberOut+= " thirty";
                                break;
                            case '4':
                                numberOut+= " fourty";
                                break;
                            case '5':
                                numberOut+= " fifty";
                                break;
                            case '6':
                                numberOut+= " sixty";
                                break;
                            case '7':
                                numberOut+= " seventy";
                                break;
                            case '8':
                                numberOut+= " eighty";
                                break;
                            case '9':
                                numberOut+= " ninty";
                                break;
                            default:
                                numberOut+= "";
                        }
                }
        }
        public static void getHunName(char[] _numberArray){
                if(_numberArray.length > 2){  //prevent error, check length
                      switch(_numberArray[_numberArray.length - 3]){
                            case '1':
                                numberOut+= " one hundred";
                                break;
                            case '2':
                                numberOut+= " two hundred";
                                break;
                            case '3':
                                numberOut+= " three hundred";
                                break;
                            case '4':
                                numberOut+= " four hundred";
                                break;
                            case '5':
                                numberOut+= " five hundred";
                                break;
                            case '6':
                                numberOut+= " six hundred";
                                break;
                            case '7':
                                numberOut+= " seven hundred";
                                break;
                            case '8':
                                numberOut+= " eight hundred";
                                break;
                            case '9':
                                numberOut+= " nine hundred";
                                break;
                            default:
                                numberOut+= "";
                        }
                }
        }
        public static void getThouName(char[] _numberArray){
                if(_numberArray.length > 3){  //prevent error, check length
                      switch(_numberArray[_numberArray.length - 4]){
                            case '1':
                                numberOut+= " one thousand";
                                break;
                            case '2':
                                numberOut+= " two thousand";
                                break;
                            case '3':
                                numberOut+= " three thousand";
                                break;
                            case '4':
                                numberOut+= " four thousand";
                                break;
                            case '5':
                                numberOut+= " five thousand";
                                break;
                            case '6':
                                numberOut+= " six thousand";
                                break;
                            case '7':
                                numberOut+= " seven thousand";
                                break;
                            case '8':
                                numberOut+= " eight thousand";
                                break;
                            case '9':
                                numberOut+= " nine thousand";
                                break;
                            default:
                                numberOut+= "";
                        }
                }
        }
    }
    Last edited by alacn; 06-24-2010 at 12:53 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  11. #11
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    ive quickly commeted the above code.

    The code basically works by entering a number into the console.
    The number is converted into a char array, which basically seperates each number into its own array. for example 213 would be seperated into
    array[0] = 2 //hundredth
    array[1] = 1 //tenth
    array[2] = 3 //unit

    take note that if i entered a number with different amount of figures such as 24. then

    array[0] = 2 //tenth
    array[1 = 4 // unit

    notice how the tenth has now changed places from array[1] to array[0] ? the code below handles this by doing array.length - 2. so in the first example the array is length 3. if we do 3 - 2 then its 1 or array[1]

    once the number is put into a char array its fed into 4 methods, each method looks at the unit and gives it a string value, ie if it sees '2' it gives it a value of 'two'.
    It stores the whole number in a global variable which adds to itself. for example, the global variable is given the value "two hundred" from the hundred method then in the tenth method its given the value "forty", and this is just added onto the "two hundred" making it "two hundred forty".

    if you analyze the code theres a bit which handles if there thenth unit is a '1'. basically the code looks at the digit where the tenth unit is and if its a 1 then it treats it different, because then the codes gotta write stuff like "twelve", "thirteen".

    At the end the String is just outputted to the console






    PHP Code:
    package chararray;
    
    import java.util.Scanner;
    
    public class Main {
        static String numberOut = "";
    
        public static void main(String[] args) {
    
            System.out.println("Enter number");
            Scanner sc = new Scanner(System.in);
            String numberTyped = sc.nextLine();
            char[] numberArray = numberTyped.toCharArray(); //changes the string entered into an array of characters
    
                getThouName(numberArray);
                getHunName(numberArray);
                getTenName(numberArray);
                getUnitName(numberArray);
    
           System.out.println(numberOut + "\n");
        }
        public static void getUnitName(char[] _numberArray){
    
            if(_numberArray.length > 1){  //prevent error, check length
                //check to see if the tenth unit was a '1', if so then does eleven,twelve etc 
                if(_numberArray[_numberArray.length - 2] == '1'){ 
                        switch(_numberArray[_numberArray.length - 1]){  
                            case '1':
                                numberOut+=  " eleven";
                                break;
                            case '2':
                                numberOut+=  " twelve";
                                break;
                            case '3':
                                numberOut+=  " thirteen";
                                break;
                            case '4':
                                numberOut+=  " fourteen";
                                break;
                            case '5':
                                numberOut+=  " fifteen";
                                break;
                            case '6':
                                numberOut+=  " sixteen";
                                break;
                            case '7':
                                numberOut+=  " seventeen";
                                break;
                            case '8':
                                numberOut+=  " eighteen";
                                break;
                            case '9':
                                numberOut+=  " nineteen";
                                break;
                            default:
                                numberOut+=  "";
                        }
                }else{
                        singleWrite(_numberArray); 
    //if tenth unit isnt a one call ther singlewrite method which just writes one,two,three etc
                }
            }else{
               singleWrite(_numberArray);
    //if the user has just entered a single number in, then the length of the array will just be 1 so this else will be  
    //called instead and the method singlewrite will be called which just writes one,two,three etc
            }                                        
    
        }
        public static void singleWrite(char[] _numberArray){
                       switch(_numberArray[_numberArray.length - 1]){
                            case '1':
                                numberOut+=  " one";
                                break;
                            case '2':
                                numberOut+=  " two";
                                break;
                            case '3':
                                numberOut+=  " three";
                                break;
                            case '4':
                                numberOut+=  " four";
                                break;
                            case '5':
                                numberOut+=  " five";
                                break;
                            case '6':
                                numberOut+=  " six";
                                break;
                            case '7':
                                numberOut+=  " seven";
                                break;
                            case '8':
                                numberOut+=  " eight";
                                break;
                            case '9':
                                numberOut+=  " nine";
                                break;
                            default:
                                numberOut+=  "";
                        }
        }
        public static void getTenName(char[] _numberArray){
                if(_numberArray.length > 1){
    //prevent error, check length of number is atleast 2 characters long otherwise you will get an error  
                      
                      switch(_numberArray[_numberArray.length - 2]){  
     //the tenth digit is always 2 digits from the right hence then -2.
    //in the below method it doesnt handle case '1' because this is handle in the   
     //getUnitName method                                                                                   
                            case '2':                                                   
                                numberOut+= " twenty";
                                break;
                            case '3':
                                numberOut+= " thirty";
                                break;
                            case '4':
                                numberOut+= " fourty";
                                break;
                            case '5':
                                numberOut+= " fifty";
                                break;
                            case '6':
                                numberOut+= " sixty";
                                break;
                            case '7':
                                numberOut+= " seventy";
                                break;
                            case '8':
                                numberOut+= " eighty";
                                break;
                            case '9':
                                numberOut+= " ninty";
                                break;
                            default:
                                numberOut+= "";
                        }
                }
        }
        public static void getHunName(char[] _numberArray){
                if(_numberArray.length > 2){  //prevent error, check length
                      switch(_numberArray[_numberArray.length - 3]){
                            case '1':
                                numberOut+= " one hundred";
                                break;
                            case '2':
                                numberOut+= " two hundred";
                                break;
                            case '3':
                                numberOut+= " three hundred";
                                break;
                            case '4':
                                numberOut+= " four hundred";
                                break;
                            case '5':
                                numberOut+= " five hundred";
                                break;
                            case '6':
                                numberOut+= " six hundred";
                                break;
                            case '7':
                                numberOut+= " seven hundred";
                                break;
                            case '8':
                                numberOut+= " eight hundred";
                                break;
                            case '9':
                                numberOut+= " nine hundred";
                                break;
                            default:
                                numberOut+= "";
                        }
                }
        }
        public static void getThouName(char[] _numberArray){
                if(_numberArray.length > 3){  //prevent error, check length
                      switch(_numberArray[_numberArray.length - 4]){
                            case '1':
                                numberOut+= " one thousand";
                                break;
                            case '2':
                                numberOut+= " two thousand";
                                break;
                            case '3':
                                numberOut+= " three thousand";
                                break;
                            case '4':
                                numberOut+= " four thousand";
                                break;
                            case '5':
                                numberOut+= " five thousand";
                                break;
                            case '6':
                                numberOut+= " six thousand";
                                break;
                            case '7':
                                numberOut+= " seven thousand";
                                break;
                            case '8':
                                numberOut+= " eight thousand";
                                break;
                            case '9':
                                numberOut+= " nine thousand";
                                break;
                            default:
                                numberOut+= "";
                        }
                }
        }
    }
    Last edited by alacn; 06-24-2010 at 12:52 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  12. #12
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Did this a while ago using enum/array. The enum version is extremely inefficient.
    It goes up to Long.MAX_VALUE
    Java Code:
    public class NumberConverter {
    
    	static final String[] oneToTwenty = { "one", "two", "three", "four", "five",
    		"six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
    		"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty" };
    	static final String[] inBetween = {"twenty", "thirty", "fourty", "fifty", 
    		"sixty", "seventy", "eighty", "ninety" };
    	static final String[] theRest = {"ten", "hundred", "thousand", "million", 
    		"billion", "trillion", "quadrillion", "quintillion", "sextillion" };
    	
    	public static String compute(String number) {
    		return compute(Long.valueOf(number));
    	}
    	public static String compute(long number) {
    		if (number <= 20)
    			if (number == 0)
    				return "";
    			else 
    				return oneToTwenty[((int)number) - 1];
    		else if (number < 100)
    			return inBetween[((int)number/10)-2] + " " + compute(number % 10);
    		else {
    			String result = "";
    			char[] numbers = String.valueOf(number).toCharArray();
    			for (int k = 0; k < numbers.length-2; k++) {
    				if (Integer.valueOf(String.valueOf(numbers[k])).intValue() == 0)
    					continue;
    				int _10or100 = (numbers.length - k <= 3) ? 0 : ((numbers.length - k - 1) % 3);
    				String _10or100String = "";
    				for (int ki = 0; ki <= _10or100; ki++, k++)
    					_10or100String+= numbers[k];
    				k--;
    				int _10or100Integer = Integer.valueOf(_10or100String);
    				result+= compute(_10or100Integer) + " " + getTheRest(numbers.length - k) +
    					" ";
    			}
    			result+= compute(number % 100);
    			return (result.charAt(result.length()-1) == 32) ? result.substring(0,result.length()-1) : result;
    		}
    	}
    	private static String getTheRest(int length) {
    		if (((length-1) % 3) == 0 && length > 6) 
    			return theRest[(((length -1) / 3)+1)];
    		return theRest[length-2];
    	}
    }
    DriverProgram:
    Java Code:
    import java.util.Scanner;
    public class NumberConverterDriver {
    	
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println(Long.MAX_VALUE);
    		System.out.println(NumberConverter.compute(Long.MAX_VALUE));
    		System.out.println("Type a number: ");
    		System.out.println(NumberConverter.compute(input.next()));
    	}
    }
    Last edited by Lil_Aziz1; 06-24-2010 at 09:14 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  13. #13
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default

    r035198x, I manage to break down the whole code into smaller method just like what you advised me to. This is my first try. The operations that I used here:
    Java Code:
     
                thousands = total / 1000;
                left = total % 1000;
                
                hundreds = left / 100;
                left = left % 100;
    
                tens = left / 10;
                left = left % 10;
                
                ones = left;
    Are converted into acessor methods. Please tell me if this is necessary:

    Java Code:
    public class NumberWord {
        private int total;
        private int ones, tens, hundreds,thousands,left;
        
        public void setThousands(int th) {
            th = thousands;
            thousands = total / 1000;
        }
        
        public int getThousands() {
            return thousands;
        }
        public void setHundreds(int h) {
            h = hundreds;
            left = total % 1000;
            hundreds = left / 100;
            
        }
        public int getHundreds() {
            return hundreds;
            
        }
        public void setTens(int tn) {
            tn = tens;
            left = total % 100;
            tens = left / 10;
        }
        public int getTens() {
            return tens;
            
        }
        public void setOnes(int o) {
            o = ones;
            ones = left;
        }
        public int getOnes() {
            return ones;
            
        }
       
    }
    So far, I just figured out just 10% on how to make my code cleaner and more efficient. Please let me know if I'm wrong. Thank you for your advice, r035198x ;)

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Nah, those accessors are not necessary. Read my first reply again and try to implement that method.

  15. #15
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 12
    Last Post: 04-16-2010, 02:18 PM
  3. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  4. Word to xml Conversion
    By kushagra in forum Advanced Java
    Replies: 3
    Last Post: 10-16-2008, 08:23 AM
  5. number to word with decimals
    By fertas in forum New To Java
    Replies: 3
    Last Post: 08-30-2008, 03:12 AM

Tags for this Thread

Posting Permissions

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