将JOptionPane输入转换为整数

时间:2014-04-30 21:57:36

标签: java swing joptionpane text-parsing

我正在尝试编写一个简单的程序,要求用户在1到10之间输入一个数字,然后显示数字。现在我已经参与了工作,如果数字超出了它再次询问的范围,但我似乎无法再次询问是否输入了一个数字,例如%或{ {1}}。

源代码:(剪掉顶部)

hello

主要问题是:

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

我似乎无法正确转换数据值。我已经想过使用if语句确定数字是否为整数,但我无法弄清楚如何检查。我希望我能做到:

number = Integer.parseInt(stringInput);

正如您所看到的,我仍然是Java的新手,感谢您花时间阅读,感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您需要使用try / catch块围绕对Integer.parseInt()的调用以检测无效输入,例如:

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

这是一个解决方案:

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());

答案 1 :(得分:1)

  

我试图编写一个要求用户使用的简单程序   输入1到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();  
            }  
        });  
    }  
}