I would like to get some help on converting an integer to word. I am very new to Java, i kind of have an idea of what i think im going to be doing but need some help with it.
The code should print the words of number from 0-999 and once -1 typed in the scanner the program should stop.
Like this:
Please type a number between 0 and 999: 1
ONE
Please type a number between 0 and 999: 11
ELEVEN
Please type a number between 0 and 999: 122
ONE HUNDRED AND TWENTY TWO
Please type a number between 0 and 999: 1000
NUMBER OUT OF RANGE
Please type a number between 0 and 999: -1
Thank you for using our program
I also have to use methods to make this "Cleaner"
First of all take hundreds place digit by deviding by 100 and print corresponding number by calling method numberToWord((number / 100), " HUNDRED");
since number / 100 would be in between 0 to 9 so it will print digit in word concatenated by HUNDRED.
Now you left with two digit number for that you directly call numberToWord((number % 100), " ");
since we are taking modulo 100 of number so it would pass two digit only. if (num > 19) {System.out.print(tens[num / 10] + " " + ones[num % 10]);}
then it will take tens place and print tens word concatenated by ones. else {System.out.print(ones[num]);}
directly prints word between 1 to 19 from the array.
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
int number = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Please type a number between 0 and 999 OR type -1 to exit: ");
number = scanner.nextInt();
while(number!=-1){
if(number>=0 && number<=999){
if(number==0){
System.out.print("NUMBER AFTER CONVERSION:\tZERO");
} else {
System.out.print("NUMBER AFTER CONVERSION:\t");
numberToWord(((number / 100) % 10), " HUNDRED");
numberToWord((number % 100), " ");
}
} else{
System.out.print("NUMBER OUT OF RANGE");
}
System.out.print("\nPlease type a number between 0 and 999 OR type -1 to exit: ");
number = scanner.nextInt();
}
}
public static void numberToWord(int num, String val) {
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"};
if (num > 19) {
System.out.print(tens[num / 10] + " " + ones[num % 10]);
} else {
System.out.print(ones[num]);
}
if (num > 0) {
System.out.print(val);
}
}
}
Sample output:
Please type a number between 0 and 999 OR type -1 to exit: 563
NUMBER AFTER CONVERSION: FIVE HUNDRED SIXTY THREE
Please type a number between 0 and 999 OR type -1 to exit: 45
NUMBER AFTER CONVERSION: FOURTY FIVE
Please type a number between 0 and 999 OR type -1 to exit: 6
NUMBER AFTER CONVERSION: SIX
Please type a number between 0 and 999 OR type -1 to exit: 0
NUMBER AFTER CONVERSION: ZERO
Please type a number between 0 and 999 OR type -1 to exit: -1
exit