使用 Integer.parseInt 时出现“线程”AWT-EventQueue-0“java.lang.NumberFormatException 中的异常 - Java

'Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException' when using Integer.parseInt - Java

提问人:SwillMith16 提问时间:3/25/2022 更新时间:3/25/2022 访问量:209

问:

我正在尝试创建一个简单的数字猜谜 GUI 游戏。我使用“Integer.parseInt”将用户的输入作为整数而不是字符串获取,以便进行比较。该程序仍按预期执行,但它在控制台中出现以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:678)
    at java.base/java.lang.Integer.parseInt(Integer.java:784)
    at Window.takeGuess(Window.java:58)
    at Window$2.actionPerformed(Window.java:32)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)

第 58 行是使用 Integer.parseInt 命令的地方,第 32 行是调用 takeGuess() 函数的地方。我的代码如下所示:

public void takeGuess() {
        while (playing) {

            // retrieve input and convert to integer for comparison
            int guess = Integer.parseInt(txtGuess.getText());
            txtGuess.setText(null);

            // decide outcome of guess
            if (guess > number) {
                txtDescription.setText("Too high!");
            } else if (guess < number) {
                txtDescription.setText("Too low!");
            } else {
                win = true;
                txtDescription.setText("You win!");
                playing = false;
            }

            // increment guess counter then repeat if needed
            guessCount++;
            currentScore.setText("" + guessCount);
            System.out.println("received guess\n");
        }
    }

我最初设置了它,它给出了同样的错误。正如我所说,尽管有此错误,该程序仍然可以运行,但我显然无论如何都想修复它。谁能帮忙?txtGuess.setText(null)txtGuess.setText("")

java 线程异常

评论

3赞 Jon Skeet 3/25/2022
好吧,您正在尝试解析一个空字符串,就好像它是一个整数一样。目前尚不清楚您是如何调用的,但它可能只应该响应用户单击按钮或类似的东西 - 并且它不应该处于循环中,否则您不会给他们机会在重试之前更改他们的猜测。takeGuess()
0赞 SwillMith16 3/25/2022
@JonSkeet,该函数是通过按钮单击调用的,并且正在解析的文本是从文本字段中获取的,他们在其中输入了他们的编号。当他们放弃时,循环就是这样,它揭示了数字,playing = false,所以他们不能再猜测,直到他们点击“再次播放”,然后播放 = true。有没有更好的方法来实现这一点?
0赞 SwillMith16 3/25/2022
@JonSkeet 这实际上真的很有帮助,将 while 循环更改为 if 语句,用于检查他们是否已经赢了或放弃了。不再循环回空字符串。谢谢!

答:

0赞 SwillMith16 3/25/2022 #1

此评论有助于解决问题:

好吧,您正在尝试解析一个空字符串,就好像它是一个整数一样。 目前尚不清楚您是如何调用 takeGuess() 的,但它应该可能 仅响应用户单击按钮或类似内容 那 - 它不应该在一个循环中,否则你不会给他们 在再次尝试之前有机会改变他们的猜测。– 乔恩·斯基特

我引入了一个名为“lose”的新布尔值,如果他们放弃,它就会设置为 true。还将 while 循环更改为 if 语句,因此函数仅在 win = false 和 lose = false 时才执行某些操作,即它们仍在播放。

public void takeGuess() {

    // cannot make a guess if won the game or given up
    if (!win && !lose) {

        // retrieve input and convert to integer for comparison
        int guess = Integer.parseInt(txtGuess.getText());
        txtGuess.setText(null);


        // decide outcome of guess
        if (guess > number) {
            txtDescription.setText("Too high!");
        } else if (guess < number) {
            txtDescription.setText("Too low!");
        } else {
            win = true;
            lose = false;
            txtDescription.setText("You win!");
        }

        // increment guess counter then repeat if needed
        guessCount++;
        currentScore.setText("" + guessCount);
        System.out.println("received guess\n");
    }

}

它不再循环回去遇到要解析的空字符串,这给出了错误。