      1  import java.util.Scanner;

      2

      3  /**

      4     This program turns an integer into its English name.

      5  */

      6  public class IntegerName

      7  {

      8     public static void main(String[] args)

      9     {

     10        Scanner in = new Scanner(System.in);

     11        System.out.print("Please enter a positive integer < 1000: ");

     12        int input = in.nextInt();

     13        System.out.println(intName(input));

     14     }

     15

     16     /**

     17        Turns a number into its English name.

     18        @param number a positive integer < 1,000

     19        @return the name of the number (e.g. "two hundred seventy four")

     20     */

     21     public static String intName(int number)

     22     {

     23        int part = number; // The part that still needs to be converted

     24        String name = ""; // The name of the number

     25

     26        if (part >= 100)

     27        {

     28           name = digitName(part / 100) + " hundred";

     29           part = part % 100;

     30        }

     31

     32        if (part >= 20)

     33        {

     34           name = name + " " + tensName(part);

     35           part = part % 10;

     36        }

     37        else if (part >= 10)

     38        {

     39           name = name + " " + teenName(part);

     40           part = 0;

     41        }

     42

     43        if (part > 0)

     44        {

     45           name = name + " " + digitName(part);

     46        }

     47

     48        return name;

     49     }

     50

     51     /**

     52        Turns a digit into its English name.

     53        @param digit an integer between 1 and 9

     54        @return the name of digit ("one" ... "nine")

     55     */

     56     public static String digitName(int digit)

     57     {

     58        if (digit == 1) { return "one"; }

     59        if (digit == 2) { return "two"; }

     60        if (digit == 3) { return "three"; }

     61        if (digit == 4) { return "four"; }

     62        if (digit == 5) { return "five"; }

     63        if (digit == 6) { return "six"; }

     64        if (digit == 7) { return "seven"; }

     65        if (digit == 8) { return "eight"; }

     66        if (digit == 9) { return "nine"; }

     67        return "";

     68     }

     69

     70     /**

     71        Turns a number between 10 and 19 into its English name.

     72        @param number an integer between 10 and 19

     73        @return the name of the given number ("ten" ... "nineteen")

     74     */

     75     public static String teenName(int number)

     76     {

     77        if (number == 10) { return "ten"; }

     78        if (number == 11) { return "eleven"; }

     79        if (number == 12) { return "twelve"; }

     80        if (number == 13) { return "thirteen"; }

     81        if (number == 14) { return "fourteen"; }

     82        if (number == 15) { return "fifteen"; }

     83        if (number == 16) { return "sixteen"; }

     84        if (number == 17) { return "seventeen"; }

     85        if (number == 18) { return "eighteen"; }

     86        if (number == 19) { return "nineteen"; }

     87        return "";

     88     }

     89

     90     /**

     91        Gives the name of the tens part of a number between 20 and 99.

     92        @param number an integer between 20 and 99

     93        @return the name of the tens part of the number ("twenty" ... "ninety")

     94     */

     95     public static String tensName(int number)

     96     {

     97        if (number >= 90) { return "ninety"; }

     98        if (number >= 80) { return "eighty"; }

     99        if (number >= 70) { return "seventy"; }

    100        if (number >= 60) { return "sixty"; }

    101        if (number >= 50) { return "fifty"; }

    102        if (number >= 40) { return "forty"; }

    103        if (number >= 30) { return "thirty"; }

    104        if (number >= 20) { return "twenty"; }

    105        return "";

    106     }

    107  }