[英]java.lang.NumberFormatException: For input string: “” javafx
public void calculer(ActionEvent event) {
String i1 = txt1.getText();
int tx1 = Integer.parseInt(i1);
t1.setText(Integer.toString(tx1));
String i2 = txt2.getText();
int tx2 = Integer.parseInt(i2);
t2.setText(Integer.toString(tx2));
String i3 = txt3.getText();
int tx3 = Integer.parseInt(i3);
t3.setText(Integer.toString(tx3));
String i4 = txt4.getText();
int tx4 = Integer.parseInt(i4);
t4.setText(Integer.toString(tx4));
String i5 = txt5.getText();
int tx5 = Integer.parseInt(i5);
t5.setText(Integer.toString(tx5));
}
This code throws the following exception:此代码引发以下异常:
Caused by: java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at fx2/sample.Controller.calculer(Controller.java:98)
... 62 more
You can check if the field is empty and if so, assign 0 to the variable, for example (because you mentioned that you want to create a bill).您可以检查该字段是否为空,如果是,则将 0 分配给变量,例如(因为您提到要创建账单)。
For this, change your code snippets like in this example with i2:为此,使用 i2 更改您的代码片段,如下例所示:
String i2 = txt2.getText();
int tx2 = Integer.parseInt(i2);
t2.setText(Integer.toString(tx2));
to至
String i2 = txt2.getText();
int tx2 = 0;
if(i2.length() > 0) {
tx2 = Integer.parseInt(i2);
}
t2.setText(Integer.toString(tx2));
You can do this with all code snippets you have (for i1 to i5).您可以使用您拥有的所有代码片段(对于 i1 到 i5)来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.