hi can you please improve my code.. thanks hope you can all help me...
here's my codes:
import java.util.Scanner;
public class NumberToWord
{
public static final String[] DIGITS = {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
public static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
public static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
// use for every grouping of 10^3 - thousands, millions, billions, etc.
public static String wordifyNumber(int number)
{
StringBuilder sb = new StringBuilder();
int jbd = number / 100;
if (jbd > 0)
{ // something in hundreds column
sb.append(DIGITS[jbd - 1] + " hundred");
}
jbd = number % 100;
int tens = jbd / 10;
if (tens > 0) { // something in tens column
if (sb.length() > 0)
{
sb.append(" ");
}
if (tens > 1)
{
sb.append(TENS[tens - 1]);
} else
{
sb.append(TEENS[jbd - 10]);
// TEENS accounts for tens + digit; done!
number = 0;
}
}
jbd = number % 10;
if (jbd > 0)
{ // something in digits column
if (sb.length() > 0)
{
if (tens >= 2)
{ // twenty-, thirty-, etc.
sb.append("-");
}
else
{
sb.append(" ");
}
}
sb.append(DIGITS[jbd - 1]);
}
return sb.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a number (or -1 to quit): ");
int number = scanner.nextInt();
if (number == -1) {
break;
} else if (number == 0) {
System.out.println("Number = zero");
} else {
System.out.print("Number = ");
if (number > 999) {
System.out.print(
wordifyNumber(number / 1000) + " thousand");
number = number % 1000;
System.out.print(number > 99 ? ", " : " ");
}
System.out.println(
wordifyNumber(number));
}
}
System.out.println("Bye.");
}
}