In: Computer Science
The question asks to allow the user to integer ranging from 1 to 999 using JOPtionpane in JAVA; and display those integers in words. For example, if the user enters 13 the program should display " Thirteen" and loop to ask the user to enter another integers. If the user enters 0, the program should quit. If the user enters an integer greater than 999, the program should display an error message and ask the user to enter an integer less than 999. Here is the code that I have tried. But, it couldn't be functional. Any help is appreciated.
```
import javax.swing.JOptionPane;
public class practice_9_1 {
public static void main(String[] args) {
int number;
String input_string= JOptionPane.showInputDialog("Please enter a
number <1000 OR 0 to exit: ");
number = Integer.parseInt(input_string);
while(number!=0){
if(number>0 && number<=999){
if(number < 0){
JOptionPane.showMessageDialog(null, "Please enter positive number
greater than 0");
} else {
// System.out.println("NUMBER AFTER CONVERSION:\t");
numberToWord(((number / 100) % 10), " HUNDRED");
numberToWord((number % 100), " ");
}
} else{
input_string= JOptionPane.showInputDialog("..DATA ERROR.. "
+"\nplease enter a number
<1000");
number = Integer.parseInt(input_string);
}
}
}
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) {
JOptionPane.showMessageDialog(null,tens[num / 10] + " " + ones[num
% 10]);
} else {
JOptionPane.showMessageDialog(null, ones[num]);
}
// if (num > 0) {
//
JOptionPane.showMessageDialog(null,val);
// }
}
}
```
import javax.swing.JOptionPane;
public class practice_9_1 {
public static void main(String[] args) {
int number;
String input_string= JOptionPane.showInputDialog("Please enter a number <1000 OR 0 to exit: ");
number = Integer.parseInt(input_string);
while(number!=0){
if(number>0 && number<=999){
if(number < 0){
JOptionPane.showMessageDialog(null, "Please enter positive number greater than 0");
} else {
// System.out.println("NUMBER AFTER CONVERSION:\t");
// numberToWord(((number / 100) % 10), " HUNDRED");
// numberToWord((number % 100), " ");
numberToWord(number);
}
} else{
input_string= JOptionPane.showInputDialog("..DATA ERROR.. "
+"\nplease enter a number <1000");
number = Integer.parseInt(input_string);
}
number = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number <1000 OR 0 to exit: "));
}
}
public static void numberToWord(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"};
int [] arr = new int[3];
arr[0] = num/100;
arr[1] = (num%100);
arr[2] = num%10;
String output = "";
if(arr[0] != 0){
output += ones[arr[0]] ;
output += " HUNDRED";
}
if(arr[1] != 0){
if(arr[1] > 19)
output += tens[arr[1]/10];
else{
output += ones[arr[1]];
arr[2] = 0;
}
}
if(arr[2] != 0){
output += ones[arr[2]];
}
JOptionPane.showMessageDialog(null,output);
// if (num > 19) {
// JOptionPane.showMessageDialog(null,tens[num / 10] + " " + ones[num % 10]);
// } else {
// JOptionPane.showMessageDialog(null, ones[num]);
// }
// if (num > 0) {
// JOptionPane.showMessageDialog(null,val);
// }
}
}