Converting JOptionPane input to integer

24,058

Solution 1

You need to surround the call to Integer.parseInt() with a try/catch block to detect invalid input, like:

try {
    number = Integer.parseInt(stringInput);
} catch (NumberFormatException e) {
    // Not a number, display error message...
}

Here is a solution:

String errorMessage = "";
do {
    // Show input dialog with current error message, if any
    String stringInput = JOptionPane.showInputDialog(errorMessage + "Enter number.");
    try {
        int number = Integer.parseInt(stringInput);
        if (number > 10 || number < 0) {
            errorMessage = "That number is not within the \n" + "allowed range!\n";
        } else {
            JOptionPane
                .showMessageDialog(null, "The number you chose is " + number + ".");
            errorMessage = ""; // no more error
        }
    } catch (NumberFormatException e) {
        // The typed text was not an integer
        errorMessage = "The text you typed is not a number.\n";
    }
} while (!errorMessage.isEmpty());

Solution 2

I'm trying to write a simple program that will ask for the user to enter a number in-between 1 and 10

.

import java.awt.EventQueue;  
import javax.swing.Icon;  
import javax.swing.JOptionPane;  
import javax.swing.UIManager;  

public class MyOptionPane {  

    public MyOptionPane() {  
        Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");  
        Object[] possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  
        Integer i = (Integer) JOptionPane.showOptionDialog(null,   
                null,  "ShowInputDialog",   
                JOptionPane.PLAIN_MESSAGE, 1,  errorIcon, possibilities, 0);

        // or

        Integer ii = (Integer) JOptionPane.showInputDialog(null,  
                "Select number:\n\from JComboBox", "ShowInputDialog",  
                JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");  
    }  

    public static void main(String[] args) {  
        EventQueue.invokeLater(new Runnable() {  
            @Override  
            public void run() {  
                MyOptionPane mOP = new MyOptionPane();  
            }  
        });  
    }  
}
Share:
24,058
Aetharius
Author by

Aetharius

Updated on July 29, 2022

Comments

  • Aetharius
    Aetharius almost 2 years

    I'm trying to write a simple program that will ask for the user to enter a number in-between 1 and 10, then display the number. Now I have gotten part to work where if the number is outside of the range it asks again, but I can't seem to get it to ask again if anything aside from a number is inputted, such as % or hello.

    The source code: (Cut out the top)

    public static void main(String[] args){
    
        int number;                 //For holding the number
        String stringInput;         //For holding the string values until converted
    
    
        //------------------//
        //Introducing the user
    
        JOptionPane.showMessageDialog(null, "This is a program that will ask \n"
                                            + "you to enter a number in-between \n"
                                            + "1-10, if the number is not within \n"
                                            + "the parameters, the program will repeat.");
    
    
        //---------------------//
        //Get input from the user
    
        stringInput = JOptionPane.showInputDialog("Enter number.");
    
        number = Integer.parseInt(stringInput);
    
    
        //-----------------//
        //Checking the number
    
        while (number > 10 || number < 0){
    
            stringInput = JOptionPane.showInputDialog("That number is not within the \n"
                                                      + "allowed range! Enter another number.");
    
            number = Integer.parseInt(stringInput);
    
        }
    
    
        //-------------------//
        //Displaying the number
    
        JOptionPane.showMessageDialog(null, "The number you chose is "
                                            + number
                                            + ".");
    
    
        //-------------//
        //Closing it down
            System.exit(0);
    }
    

    The main problem is the:

    number = Integer.parseInt(stringInput);
    

    I can't seem to convert the data values properly. I already thought of something like using an if statement to determine if the number is an integer, but I couldn't figure out how to check. I wish I could do:

    if (number == Integer)
    

    As you can see I am still extremely new to Java, any help is appreciated, thanks for taking the time to read.