Question

In Java JOptionPane how do you validate if a user enters an integer value? How do...

In Java JOptionPane how do you validate if a user enters an integer value? How do you reprompt the user to enter an integer if they first entered a double or a string value?

int length = Integer.parseInt(JOptionPane.showInputDialog("Enter the length: "));

int width = Integer.parseInt(JOptionPane.showInputDialog("Enter the width: "));

int height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height: "));

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import javax.swing.*;

public class ValidateIntegerInput {

    public static void main(String[] args) {
        int length, width, height;
        while (true) {
            try {
                length = Integer.parseInt(JOptionPane.showInputDialog("Enter the length: "));
                break;
            } catch (Exception e) {

            }
        }
        while (true) {
            try {
                width = Integer.parseInt(JOptionPane.showInputDialog("Enter the width: "));
                break;
            } catch (Exception e) {

            }
        }
        while (true) {
            try {
                height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height: "));
                break;
            } catch (Exception e) {

            }
        }
        JOptionPane.showMessageDialog(null, "Length: " + length + ", Width: " + width + ", Height: " + height);
    }
}
Add a comment
Know the answer?
Add Answer to:
In Java JOptionPane how do you validate if a user enters an integer value? How do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will turn in a java file named "MyMethods.java". It will contain one class named "MyMethods". That class will contain three methods. These methods must be in JOptionPane and must c...

    You will turn in a java file named "MyMethods.java". It will contain one class named "MyMethods". That class will contain three methods. These methods must be in JOptionPane and must contain: getAnInt will return a value of type int, and take as an argument a string to prompt the user. The actual prompt presented to the user should include not only the string argument, but also the information that the user may press Cancel or enter an empty string to...

  • ANS (2) 0R hat output is produced by the following code if user enters these values...

    ANS (2) 0R hat output is produced by the following code if user enters these values for a question asked by the progran .User entered 3 Enter a positive integer value! (enter -1 to end) :3 Enter a positive integer value! (enter -1 to end) five Enter a positive integer value! (enter -1 to end) :7 Enter a positive integer value! (enter -1 to end) :-1 User entered five -User entered 7 ...User entered -1 import java.util.Scanner: class Verify input...

  • Which of the following verifies that the user enters a positive integer value? A. int num...

    Which of the following verifies that the user enters a positive integer value? A. int num = 0: Scanner s = new Scanner(System.in): do { System.out.print("Enter a positive integer: "): num = s.nextlnt(): } while (num = = 0): B. int num=0: Scanner s = new Scanner(System.in): do { System.out.print("Enter a positive integer: "): num = s.nextlnt(): } while (num % 2 ! = 0): C. int num=0: Scanner s = new Scanner(System.in): do { System.out.print("Enter a positive integer: "):...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • Introducing JOptionPane. The Learning Goal for this exercise is to interact with a user with JOptionPane...

    Introducing JOptionPane. The Learning Goal for this exercise is to interact with a user with JOptionPane and use that instead of Scanner for user input and output. PowerShell or Terminal is not a standard way that users interact with programs. Windows and dialog boxes are very natural. JOptionPane will allow you to get information to and from a user. Create a Calculator that will calculate the grade in this course. _Create a Java Program that can calculate your final grade...

  • 12 Combine these two statements into one GUI staterment to get the integer value of from...

    12 Combine these two statements into one GUI staterment to get the integer value of from user input: String tStr JOptionPane.showinput Dialog ("Enter an integer value "k int t " Integer.parseInt ( tSt ANS: Line(l) 13 What is the O/P produced by the following code double ilR double il double bB 2000; double ell " bB . ( 1 + iR ) 0.05 IR 100 interest rate per year ./ show interest rate impercentage (5%) / begin balance anst System.out,printf...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT