0

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;
}
}
```

*Edited 3 Years Ago by pyTony*: fixed formating