1

My program is to convert numbers between 0 and 100 to words. I'm not sure which strategy would be best to go about this but I would like to use math operators like modules, etc.This is what I've got so far.

package project.pkg1;

import java.util.Scanner;

public class Project1 {
public static final int MINUMUN_NUMBER = 0;
public static final int MAXIMUM_NUMBER = 100;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int num, x;
do{
    System.out.println("Enter a number between 0 and 100: ");
    num = keyboard.nextInt();

} while ((num < 0) || (num > 100));
if(num == 0)
        System.out.println("zero");
if(num == 100)
        System.out.println("one hundred");

I don't want to use the if(num == x) every time. My validation trap to make sure it is between 0 and 100 works fine. I'm just not sure how to approach the rest of it. This is what I can get to print:

run:
Enter a number between 0 and 100: 
-1
Enter a number between 0 and 100: 
101
Enter a number between 0 and 100: 
34
BUILD SUCCESSFUL (total time: 23 seconds)

I need that 34 to print "thirty four" Any feedback would be greatly appreciated!

user3338808
  • 11
  • 1
  • 2

3 Answers3

1

You should look at what the pattern is for numbers from 0 to 100. Here is what you would find:

  • single digit numbers: pull from String[] digit = {"zero", "one", two, ... , "ten" }
  • for teens pull from String[] teen = {"eleven", "twelve", ... ,"nineteen"}
  • for larger numbers divide by 10 and pull from {"twenty", "thirty", ... "ninety"}; then, if number has a remainder when divided by ten, add digit portion "-" + pull from digit above.
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
0

You might as well create separate variables for the numbers from 1-19 and 100. As for 20-99, these have prefixes and suffixes that can be combined using string concatenation and can be identified through the first and last digits.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0
public class NumbersToWords {

    private static final String[] NUMBERS = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
        "one hundred"
    };

    private static final String[] TENS = {
        "twenty",
        "thirty",
        "fourty",
        "fifty",
        "sixty",
        "seventy",
        "eighty",
        "ninty"
    };

    public static String numberToWord( final int number ){
        if ( number < 0 )
            throw new IllegalArgumentException( "Number too low" );
        if ( number > 100 )
            throw new IllegalArgumentException( "Number too high" );
        if ( number < 20 )
            return NUMBERS[number];
        if ( number == 100 )
            return NUMBERS[20];
        return TENS[number/10-2] + ((number%10>0)?(" "+NUMBERS[number%10]):"");
    }

    public static void main(String[] args) {
        for ( int i = 0; i <= 100; ++i )
            System.out.println( numberToWord( i ) );
    }
}
MT0
  • 143,790
  • 11
  • 59
  • 117
  • I really appreciate the code! But, I'm trying to get what your code just output to be my answer. I need it to output Enter a number between 0 and 100: 34 Thirty Four – user3338808 Feb 21 '14 at 21:10