Conver number to words (thousands) help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JanineXD
    New Member
    • Feb 2010
    • 18

    Conver number to words (thousands) help

    Hello,

    I already have the program ( convert number to words but only 0-999 ). I need it to be ( 0-9999 ).

    can you help me?

    Code:
    package num2word;
    
    import java.util.Scanner;
    
    public class NumbertoWords {
    
        // units Method
        public static void units(int n) {
    
            switch (n) {
                case 1:
                    System.out.print("one ");
                    break;
                case 2:
                    System.out.print("two ");
                    break;
                case 3:
                    System.out.print("three ");
                    break;
                case 4:
                    System.out.print("four ");
                    break;
                case 5:
                    System.out.print("five ");
                    break;
                case 6:
                    System.out.print("six ");
                    break;
                case 7:
                    System.out.print("seven ");
                    break;
                case 8:
                    System.out.print("eight ");
                    break;
                case 9:
                    System.out.print("nine ");
                    break;
            }
        }
    
        // special case Method
        public static void special(int n) {
    
            switch (n) {
                case 11:
                    System.out.print(" eleven");
                    break;
                case 12:
                    System.out.print(" twelve");
                    break;
                case 13:
                    System.out.print(" thirteen");
                    break;
                case 14:
                    System.out.print(" fourteen");
                    break;
                case 15:
                    System.out.print(" fifteen");
                    break;
                case 16:
                    System.out.print(" sixteen");
                    break;
                case 17:
                    System.out.print(" seventeen");
                    break;
                case 18:
                    System.out.print(" eighteen");
                    break;
                case 19:
                    System.out.print(" nineteen");
                    break;
            }
        }
    
        // tens Method
        public static void tens(int n) {
    
            switch (n) {
                case 1:
                    System.out.print(" ten ");
                case 2:
                    System.out.print(" twenty ");
                    break;
                case 3:
                    System.out.print(" thirty ");
                    break;
                case 4:
                    System.out.print(" forty ");
                    break;
                case 5:
                    System.out.print(" fifty ");
                    break;
                case 6:
                    System.out.print(" sixty ");
                    break;
                case 7:
                    System.out.print(" seventy ");
                    break;
                case 8:
                    System.out.print(" eighty ");
                    break;
                case 9:
                    System.out.print(" ninety ");
                    break;
            }
        }
    
        // hundreds Method
        public static void hundreds(int n) {
    
            switch (n) {
                case 1:
                    System.out.print("one hundred ");
                    break;
                case 2:
                    System.out.print("two hundred ");
                    break;
                case 3:
                    System.out.print("three hundred ");
                    break;
                case 4:
                    System.out.print("four hundred ");
                    break;
                case 5:
                    System.out.print("five hundred ");
                    break;
                case 6:
                    System.out.print("six hundred ");
                    break;
                case 7:
                    System.out.print("seven hundred ");
                    break;
                case 8:
                    System.out.print("eight hundred ");
                    break;
                case 9:
                    System.out.print("nine hundred ");
                    break;
            }
        }
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            // Requesting User input
            System.out.print("Enter an integer number[0 to 999]::>> ");
            int number = input.nextInt();
    
            // input validation
            while (number < 0 || number > 999) {
                System.out.println("Input Too Large, ");
                System.out.print("Enter an integer number[0 to 999]::>> ");
                number = input.nextInt();
            }
    
            // Below we start <strong class="highlight">to</strong> determine what the input was by
            // categorising it using boolean
    
            // original number (100-999)
            if (number > 99 && number < 1000) {
                int h = number / 100;    //find the hundreds only (one, two hundred)
                hundreds(h);             //print number
    
                // first part of number found & printed (hundreds)
                // remamining 2 digits must be found ie tens & units
    
                // test if they are a special number
                int x = 0;               // initialized variable x for calculations
                x = number % 100;        // find remainder of hundreds (ie x54)
                if (x > 10 && x < 20) {  // ie is 54 a special number?
                    special(x);          // print number
                }
                // if not special number -> split up into tens & units
                if (x > 0 && x < 100) {
    
                    int tens = x / 10;   // finding the tens (forty, fifty)
                    tens(tens);          // print number
    
                    int units = x % 10;  // finding the units (one, two)
                    units(units);        // print number
                }
            }
    
            // original number (20-99)
            if (number > 19 && number < 100) {
    
                int t = number / 10;     // finding the tens (forty, fifty)
                tens(t);                 // print number
    
                int u = number % 10;     // finding the units (one, two)
                units(u);                // print number
            }
    
            // original number (11-19) (Special <strong class="highlight">numbers</strong>)
            if (number > 10 && number < 20) { // (Special <strong class="highlight">numbers</strong>)
                special(number);         // print number
            }
    
            // original number (0-9)
            if (number < 10) {           // finding the units (one, two)
                units(number);           // print number
            }
        }
    }
    Thanks
  • JanineXD
    New Member
    • Feb 2010
    • 18

    #2
    Here's the updated program:
    Code:
    package num2word;
    
    import java.util.Scanner;
    
    public class NumbertoWords {
    
        // units Method
        public static void units(int n) {
    
            switch (n) {
                case 1:
                    System.out.print("one ");
                    break;
                case 2:
                    System.out.print("two ");
                    break;
                case 3:
                    System.out.print("three ");
                    break;
                case 4:
                    System.out.print("four ");
                    break;
                case 5:
                    System.out.print("five ");
                    break;
                case 6:
                    System.out.print("six ");
                    break;
                case 7:
                    System.out.print("seven ");
                    break;
                case 8:
                    System.out.print("eight ");
                    break;
                case 9:
                    System.out.print("nine ");
                    break;
            }
        }
    
        // special case Method
        public static void special(int n) {
    
            switch (n) {
                case 11:
                    System.out.print(" eleven");
                    break;
                case 12:
                    System.out.print(" twelve");
                    break;
                case 13:
                    System.out.print(" thirteen");
                    break;
                case 14:
                    System.out.print(" fourteen");
                    break;
                case 15:
                    System.out.print(" fifteen");
                    break;
                case 16:
                    System.out.print(" sixteen");
                    break;
                case 17:
                    System.out.print(" seventeen");
                    break;
                case 18:
                    System.out.print(" eighteen");
                    break;
                case 19:
                    System.out.print(" nineteen");
                    break;
            }
        }
    
        // tens Method
        public static void tens(int n) {
    
            switch (n) {
                case 1:
                    System.out.print(" ten ");
                case 2:
                    System.out.print(" twenty ");
                    break;
                case 3:
                    System.out.print(" thirty ");
                    break;
                case 4:
                    System.out.print(" forty ");
                    break;
                case 5:
                    System.out.print(" fifty ");
                    break;
                case 6:
                    System.out.print(" sixty ");
                    break;
                case 7:
                    System.out.print(" seventy ");
                    break;
                case 8:
                    System.out.print(" eighty ");
                    break;
                case 9:
                    System.out.print(" ninety ");
                    break;
            }
        }
    
        // hundreds Method
        public static void hundreds(int n) {
    
            switch (n) {
                case 1:
                    System.out.print("one hundred ");
                    break;
                case 2:
                    System.out.print("two hundred ");
                    break;
                case 3:
                    System.out.print("three hundred ");
                    break;
                case 4:
                    System.out.print("four hundred ");
                    break;
                case 5:
                    System.out.print("five hundred ");
                    break;
                case 6:
                    System.out.print("six hundred ");
                    break;
                case 7:
                    System.out.print("seven hundred ");
                    break;
                case 8:
                    System.out.print("eight hundred ");
                    break;
                case 9:
                    System.out.print("nine hundred ");
                    break;
            }
        }
        // hundreds Method
        public static void thousands(int n) {
    
            switch (n) {
                case 1:
                    System.out.print("one thousand ");
                    break;
                case 2:
                    System.out.print("two thousand ");
                    break;
                case 3:
                    System.out.print("three thousand ");
                    break;
                case 4:
                    System.out.print("four thousand ");
                    break;
                case 5:
                    System.out.print("five thousand ");
                    break;
                case 6:
                    System.out.print("six thousand ");
                    break;
                case 7:
                    System.out.print("seven thousand ");
                    break;
                case 8:
                    System.out.print("eight thousand ");
                    break;
                case 9:
                    System.out.print("nine thousand ");
                    break;
            }
        }
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            // Requesting User input
            System.out.print("Enter an integer number[0 to 9999]::>> ");
            int number = input.nextInt();
    
            // input validation
            while (number < 0 || number > 9999) {
                System.out.println("Input Too Large, ");
                System.out.print("Enter an integer number[0 to 9999]::>> ");
                number = input.nextInt();
            }
    
            // Below we start <strong class="highlight">to</strong> determine what the input was by
            // categorising it using boolean
    
    // thousands (1000-9999)
            if (number > 999 && number < 10000) {
                int t = number / 1000;    //find the hundreds only (one, two hundred)
                thousands(t);             //print number
    
    
                // first part of number found & printed (hundreds)
                // remamining 2 digits must be found ie tens & units
    
                // test if they are a special number
                int x = 0;               // initialized variable x for calculations
                x = number % 1000;        // find remainder of hundreds (ie x54)
                if (x > 10 && x < 20) {  // ie is 54 a special number?
                    special(x);          // print number
                }
                // if not special number -> split up into tens & units
                if (x > 0 && x < 1000) {
    
                    int hundreds = x / 100;   // finding the hundreds
                    hundreds(hundreds);          // print number
    
                    int tens = x / 10;   // finding the tens (forty, fifty)
                    tens(tens);          // print number
    
                    int units = x % 10;  // finding the units (one, two)
                    units(units);        // print number
                }
            }
            // original number (100-999)
            if (number > 99 && number < 1000) {
                int h = number / 100;    //find the hundreds only (one, two hundred)
                hundreds(h);             //print number
    
    
                // first part of number found & printed (hundreds)
                // remamining 2 digits must be found ie tens & units
    
                // test if they are a special number
                int x = 0;               // initialized variable x for calculations
                x = number % 100;        // find remainder of hundreds (ie x54)
                if (x > 10 && x < 20) {  // ie is 54 a special number?
                    special(x);          // print number
                }
                // if not special number -> split up into tens & units
                if (x > 0 && x < 100) {
    
                    int tens = x / 10;   // finding the tens (forty, fifty)
                    tens(tens);          // print number
    
                    int units = x % 10;  // finding the units (one, two)
                    units(units);        // print number
                }
            }
    
            // original number (20-99)
            if (number > 19 && number < 100) {
    
                int t = number / 10;     // finding the tens (forty, fifty)
                tens(t);                 // print number
    
                int u = number % 10;     // finding the units (one, two)
                units(u);                // print number
            }
    
            // original number (11-19) (Special <strong class="highlight">numbers</strong>)
            if (number > 10 && number < 20) { // (Special <strong class="highlight">numbers</strong>)
                special(number);         // print number
            }
    
            // original number (0-9)
            if (number < 10) {           // finding the units (one, two)
                units(number);           // print number
            }
        }
    }
    The output is:
    Code:
    Enter an integer number[0 to 999]::>> 5670
    five thousand six hundred 
    Process completed.
    I dont know where the problem is.
    help please?

    thanks!

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      I guess it would be simpler if you convert the input value into a string. access the element of that string and get the representation. ...

      Comment

      • JanineXD
        New Member
        • Feb 2010
        • 18

        #4
        like this one?
        Code:
        public class NumberToWords {
        
        	private static final String[] ONES = {
        		"Zero", "One", "Two", "Three", "Four", "Five",
        		"Six", "Seven", "Eight", "Nine" };
        	private static final String[] TEENS = {
        		"Ten", "Eleven", "Twelve", "Thirteen", null, "Fifteen",
        		null, null, "Eighteen", null };
        	private static final String[] TENS = {
        		null, null, "Twenty", "Thirty", "Forty", "Fifty",
        		"Sixty", "Seventy", "Eighty", "Ninety" };
        
        	public static String numberToWords(int number) {
        		if (number<10) {
        			return ONES[number];
        		} else if (number<20) {
        			int n = number - 10;
        			String words = TEENS[n];
        			return words==null ? ONES[n]+"teen" : TEENS[n];
        		} else {
        			int n = number % 10;
        			return TENS[number/10] +
        				(n==0 ? "" : (" " + numberToWords(n)));
        		}
        	}
        
        	public static void main(String[] args) {
        		for (int i=0; i<100; i++) {
        			System.out.println(i+" "+numberToWords(i));
        		}
        	}
        }

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          First off, split the cases up smarter. eg pseudocode:
          Code:
          Currently you have:
          if (thousands){
            print thousands
            print hundreds
            print special
            print tens
            print ones
          } else {
            print hundreds
            print special
            print tens
            print ones 
          }
          
          Whereas, to simplify the code, you really should have:
          If (thousands?)
            Print thousands
          if (hundreds?)
            Print hundreds
          if (special?){
            print special
          } else {
            print tens
            print ones
          }
          ALSO!! You could simply add a case 0:
          case 0: // output nothing
          break;

          2. the way you're getting your tens digit is off.
          int tens = x / 10;
          should be
          int tens = x/10 % 10;

          You can do this for all of your number parts. eg
          thousands = number / 1000 % 10;
          hundreds = number / 100 % 10;
          tens = number / 10 % 10;
          units = number % 10;

          Comment

          Working...