Hi All,
I am a newbie of 8 weeks enjoying the challange of trying to understand java.

I was assigned to create a rather challenging program in which the user is prompted to input a number from 1 to 10,000,000 then the number outputs in english words.
For example- please enter a number from 1 to 10,000,000: 3

You entered the number three

I have developed the algorithm loosely based on IBM's Spellout. I think I am almost there. I am pretty sure the algorithm is correct. I just cant seem to get the thing to run. I would appreciate any help. Thank You.

import java.util.Scanner;

// This program translates a number from one to 10,000,000 to words that would be used when saying the number.
// It looks at the input number in three parts representing millions, thousands, and units, respectively.
// Each part is translated to the corresponding words for a three digit number and the unit, millions or
// thousands, is appended as appropriate.
public class SayNumberClass {
    public static void main () {
    //         create scanner to obtain input from command window
    Scanner input = new Scanner (System.in); 

    int number;  // Number input by user
    int oneMillion = 1000000;
    int oneThousand = 1000;

    //         Get a number from the user
    while (true) {
        System.out.print( "Enter a Number between one and 10 million:");
        number = input.nextInt();
        if(number < 1 || number > 10 * oneMillion) {
        System.out.print("Erroneous number.  Please try again.");    
        } else {
        break;
        }
    }

    // Digits in each part of the number
    int digits[3];
    // Words representing each part of the number
    String words[] = {"", "", ""};

    // Translate the rightmost part of the number (ones)
    digits[2] = number % 1000;
    words[2] = say_hundreds(digits[2]);

    // Translate the middle part of the number (thousands)
    if(number > oneThousand) {
        digits[1] = (number % oneMillion) / oneThousand;
        words[1] = say_hundreds(digits[1]) + " thousand";    
    }

    // Translate the leftmost part of the number (millions)break;
    if(number > oneMillion) {
        digits[0] = number / oneMillion;
        words[0] = say_hundreds(digits[0]) + " million";
    }


    //         Write the answer
    System.out.print(words[0] + " " + words[1] + " " +  words[2]);    
    }

    // Function to Create Words for a Number from Zero to a Hundred    
    public static String say_hundreds (int number) {

    // Return string containing words for input number
    String text = "";
    // Words representing digits from 1 - 9
    String digits[] = {"one", "two", "three", "four", "five", "six",
               "seven", "eight", "nine"};
    // Words representing tens from 20 - 90
    String tens[] = {"", "twenty", "thirty", "forty", "fifty", "sixty",
             "seventy", "eighty", "ninety"};
    // Words representing teens from 10 to 19
    String teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen",
              "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

    // Digits representing hundreds, tens, and ones positions in
    // input number, respectively
    int hDigit, tDigit, oDigit;

    // Extract individual digits
    hDigit = number / 100;
    oDigit = number % 10;
    tDigit = (number % 100) /10;

    // if there are hundreds, append words to the text
    if(hDigit > 0) {
        text += digits[hDigit-1] + " hundred";                    
    }

    // If there are tens, append words to the text
    if(tDigit > 0) {
        if(tDigit > 1) {
        text += " " + tens[tDigit-1];
        if(oDigit > 0) {
            text += "-" + digits[oDigit-1];
        }
        } else {
        text += " " + teens[oDigit-1];
        }

    } else if(oDigit > 1) {
        text += " " + digits[oDigit-1];
    }

    return text;
    }
}
Member Avatar for iamthwee

You really need to work on your code style and indent it.

import java.util.Scanner;

// This program translates a number from one to 10,000,000 to words that would be used when saying the number.
// It looks at the input number in three parts representing millions, thousands, and units, respectively.
// Each part is translated to the corresponding words for a three digit number and the unit, millions or
// thousands, is appended as appropriate.
public class SayNumberClass 
{
   public static void main (String[] args) 
   {
      // create scanner to obtain input from command window
      Scanner input = new Scanner (System.in);

      int number; // Number input by user
      int oneMillion = 1000000;
      int oneThousand = 1000;

      // Get a number from the user
      while (true) 
      {
	 System.out.print( "Enter a Number between one and 10 million:");
	 number = input.nextInt();
	 if(number < 1 || number > 10 * oneMillion) 
         {
	    System.out.print("Erroneous number. Please try again.");
	 }
         else 
         {
	    break;
	 }
      }

      // Digits in each part of the number
      int[] digits = new int[3];
      // Words representing each part of the number
      String words[] = {"", "", ""};

      // Translate the rightmost part of the number (ones)
      digits[2] = number % 1000;
      words[2] = say_hundreds(digits[2]);

      // Translate the middle part of the number (thousands)
      if(number > oneThousand) 
      {
	 digits[1] = (number % oneMillion) / oneThousand;
	 words[1] = say_hundreds(digits[1]) + " thousand";
      }

      // Translate the leftmost part of the number (millions)break;
      if(number > oneMillion) 
      {
	 digits[0] = number / oneMillion;
	 words[0] = say_hundreds(digits[0]) + " million";
      }


      // Write the answer
      System.out.print(words[0] + " " + words[1] + " " + words[2]);
   }

   // Function to Create Words for a Number from Zero to a Hundred
   public static String say_hundreds (int number) 
   {

      // Return string containing words for input number
      String text = "";
      // Words representing digits from 1 - 9
      String digits[] = {"one", "two", "three", "four", "five", "six",
	 "seven", "eight", "nine"};
      // Words representing tens from 20 - 90
      String tens[] = {"", "twenty", "thirty", "forty", "fifty", "sixty",
	 "seventy", "eighty", "ninety"};
      // Words representing teens from 10 to 19
      String teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen",
	 "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

      // Digits representing hundreds, tens, and ones positions in
      // input number, respectively
      int hDigit, tDigit, oDigit;

      // Extract individual digits
      hDigit = number / 100;
      oDigit = number % 10;
      tDigit = (number % 100) /10;

      // if there are hundreds, append words to the text
      if(hDigit > 0) 
      {
	 text += digits[hDigit-1] + " hundred";
      }

      // If there are tens, append words to the text
      if(tDigit > 0) 
      {
	 if(tDigit > 1) 
        {
	    text += " " + tens[tDigit-1];
	    if(oDigit > 0) 
           {
	       text += "-" + digits[oDigit-1];
	    }
	 } 
          else 
         {
	    text += " " + teens[oDigit-1];
	 }

      } 
      else if(oDigit > 1) 
      {
	 text += " " + digits[oDigit-1];
      }

      return text;
   }
}

[edit] A few things I noticed, it gives the wrong answer for one million -> 1000000. Also the error checking should be done whilst the input is still a string?[/edit]

Thank you for your helpful reply. I really do need work on my style and indenting. I hope to improve with time. I will have to research further on error checking to gain a better understanding.

This version works without errors and performs extra validation. Converts numbers upto virgintillion 10E63

  1. This is a 6 year old thread - the OP has certainly moved on by now
  2. Which version are you referring to when yo say "This version"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.