I am creating a program that reads numbers between 0-999 and then returns that number in words. I have got all other numbers working but I can't figure out how to get numbers between 111 - 119, 211 - 219, 311 - 319 and so on all the way to 911 - 919. Can you anyone help? Thanks!
I have tried changing numones = number%10;
to numones = number%100;
but that fails to read all other numbers and causes an array outofbounds
error.
public static void main(String[] args) {
int numones;
int numtens;
int numhundreds;
String [] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fiveteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
String [] tens = {"","", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
System.out.print("Number: ");
int number = In.nextInt();
while(number !=- 1) {
if (number >= 100 && number <= 999) {
numhundreds = number / 100;
numtens = number/10%10;
numones = number%10;
if (number == 100 || number == 200 || number == 300 || number == 400 || number == 500
|| number == 600 || number == 700 || number == 800 || number == 900){
System.out.println("" + ones[numhundreds] + " " + "hundred");
System.out.print("Number: ");
number = In.nextInt();
}else{
System.out.println("" + ones[numhundreds] + " " + "hundred and " + tens[numtens] + " " + ones[numones]);
System.out.print("Number: ");
number = In.nextInt();
}
}
}
}
}
When 111 is entered, the output is one hundred and one when it should be one hundred and eleven.