java用于输入字符串:“文本字段填写时”
我正在用Java编写一个程序,其中有一些文本字段和一个按钮
我得到了一个java.lang.NumberFormatException: For input string: ""
,尽管我在运行程序时填写了所有文本字段
我的代码如下所示:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
method();
}
}
);
public void method() {
try {
String string1 = textfield1.getText();
String string2 = textfield2.getText();
String string3 = textfield3.getText();
if ( string1.length() == 0 || string2.length() == 0 || string3.length() == 0) {
System.out.println("fill in the required text fields");
return;
}
int number = Integer.parseInt(textfield3.getText());
//do something
}
catch ( NumberFormatException e ) {
System.out.println("Wrong format");
}
}
编辑:
共 (0) 个答案