'Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException' 当使用 Integer.parseInt - Java
'Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException' when using Integer.parseInt - Java
我正在尝试创建一个简单的猜数 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.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("")
,但它给出了同样的错误。正如我所说,尽管有这个错误,程序仍然可以运行,但我显然还是想修复它。有人可以帮忙吗?
这条评论帮助解决了问题:
Well, you're trying to parse an empty string as if it's an integer.
It's not clear how you're calling takeGuess(), but it should probably
only be in response to the user clicking a button or something like
that - and it shouldn't be in a loop, otherwise you're not giving them
a chance to change their guess before trying again. – Jon Skeet
我引入了一个名为 '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");
}
}
它不再循环回遇到要解析的空字符串,这给出了错误。
我正在尝试创建一个简单的猜数 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.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("")
,但它给出了同样的错误。正如我所说,尽管有这个错误,程序仍然可以运行,但我显然还是想修复它。有人可以帮忙吗?
这条评论帮助解决了问题:
Well, you're trying to parse an empty string as if it's an integer. It's not clear how you're calling takeGuess(), but it should probably only be in response to the user clicking a button or something like that - and it shouldn't be in a loop, otherwise you're not giving them a chance to change their guess before trying again. – Jon Skeet
我引入了一个名为 '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");
}
}
它不再循环回遇到要解析的空字符串,这给出了错误。