我不断收到空字符串错误,如下所示
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
我不确定为什么会一直收到此错误,因为在JTextField中不键入数字就不会尝试计算。下面是我用于“计算”按钮的代码,这很简单。
private void ButtonCalculateActionPerformed(java.awt.event.ActionEvent evt) {
Height = Integer.parseInt(this.TextFieldHeight.getText());
Weight = Integer.parseInt(this.TextFieldWeight.getText());
BMI = Integer.parseInt(this.TextFieldBMI.getText());
answer = Height + Weight;
this.TextFieldBMI.setText("The answer is: " + answer);
}
这些是定义的变量
//variables
static int Height;
static int Weight;
static int BMI;
static int answer;
对不起,我很抱歉!我不应该添加BMI变量,因为应该将答案输出到TextFieldBMI
而不是.getText()
从那里输出,因此它始终为空。问题解决了!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句