public class NumberToWords {
public static String intToWords(int num) {
String ones [] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String tens [] = {"", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String words = "";
if (num == 0) {
return "zero";
}
else if (num >= 100) { // 112 one hundred and
words += ones[num / 100] + " " + "hundred ";
if (num % 10 != 0) {
words += "and " + tens[(num % 100)/10]; /* + " " + ones[num % 10];*/
if (num % 100 < 19 && num % 10 < 9) {
words += " " + ones[num % 10];
if (num % 10 == 0){
words += tens[(num % 100) / 10]; //AROUND THIS AREA IS WHERE THE PROBLEM OCCURS
}
// if (num % 100 > 9) {
// words += ones[(num % 100)];
// }
}
}
}
// else if (num >= 100) {
// }
else if (num > 20 && num <= 99) {
words += tens[num /10] + " " + ones[num %= 10];
}
else if (num < 20) {
words += ones[num];
}
return words.trim();
}
// public static String intToRoman(int num) {
// //Write your code here
// String [] ones = {"I", "II", "III" "IV" "V", "VI", "VII", "VIII" "IX"}
// String [] tens = {"X", "XX", "XXX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "LC", "C"}
// // char [] r = {'I', 'V', 'X', }
// }
public static void main(String[] args) {
//Write your code here
int num;
// do {
System.out.print("Enter a number between 0-999, or -1 to exit: ");
while ((num = In.nextInt()) != -1){
String words = intToWords(num);
System.out.println("In Words: "+words);
System.out.print("Enter a number between 0-999, or -1 to exit:");
}
// }while (true);
}
}
I always am only able to get either the hundreds such as '378' or '555' to work but never this AND the hundred teens such as '112' or '211' These print "one hundred and two" and two hundred and one respectively if they don't fail all together with a outside of length error. Edit: Also test case such as 366 or 244 doesn't output properly.
There are a few problems with the code that must be fixed:
Here's the corrected code:
public class NumberToWords {
public static String intToWords(int num) {
String ones [] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String tens [] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String words = "";
if (num == 0) {
return "zero";
} else if (num >= 100) {
words += ones[num / 100] + " hundred ";
if (num % 100 != 0) {
words += "and ";
if (num % 100 < 20) {
words += ones[num % 100];
} else {
words += tens[(num % 100) / 10] + " " + ones[num % 10];
}
}
} else if (num >= 20 && num <= 99) {
words += tens[num / 10] + " " + ones[num % 10];
} else if (num < 20) {
words += ones[num];
}
return words.trim();
}
public static void main(String[] args) {
int num;
System.out.print("Enter a number between 0-999, or -1 to exit: ");
Scanner scanner=new Scanner(System.in);
while ((num = scanner.nextInt()) != -1) {
String words = intToWords(num);
System.out.println("In Words: " + words);
System.out.print("Enter a number between 0-999, or -1 to exit:");
}
scanner.close();
}
}