私はいくつかのテキストフィールドとボタンを取得した 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");
}
}
編集: