Question

In: Computer Science

The question asks to allow the user to integer ranging from 1 to 999 using JOPtionpane...

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);
// }
}
}

```

Solutions

Expert Solution

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);
        // }
    }
}


Related Solutions

Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a program which asks the user for grade, in integer form, from 0 to 100....
Write a program which asks the user for grade, in integer form, from 0 to 100. If the user's response is negative or greater than 100, insult them. Otherwise, use if / else statements to print out the letter grade corresponding to the user's input. 90 - 100 : A 80 - 89 : B 70 - 79 : C 60 - 69 : D 0 - 59 : F Then, write a second version of the program which uses...
Write a program which asks the user for grade, in integer form, from 0 to 100....
Write a program which asks the user for grade, in integer form, from 0 to 100. If the user's response is negative or greater than 100, insult them. Otherwise, use if / else statements to print out the letter grade corresponding to the user's input. 90 - 100 : A 80 - 89 : B 70 - 79 : C 60 - 69 : D 0 - 59 : F Then, write a second version of the program which uses...
Checker for integer string Forms often allow a user to enter an integer. Write a program...
Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the output is: no Hint: Use a loop and the Character.isDigit() function. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr...
In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
Forms often allow a user to enter an integer. Write a programthat takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT