我正在编写一个程序,在4个文本字段框中包含4种数据类型。第一个框包含一个名称,第二个包含一个年龄,第三个电子邮件,最后一个是电话号码。我想添加一个覆盖空文本框的异常处理程序。我不知道如何编写这部分程序。有人可以帮帮我吗?这是我到目前为止所拥有的。
private void nextEntry() throws Exception
{
//Zero out all input fields
cName.setText("");
cAge.setText("");
cEmail.setText("");
cPhone.setText("");
}
private class submitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Write file to data file
try {
//Get
String name = cName.getText();
String age = cAge.getText();
String email = cEmail.getText();
String phone = cPhone.getText();
Integer intAge = Integer.parseInt(age);
//Validate Input
//If name is blank
if (name == "") {
throw InputMismatchException();
}
catch (InputMismatchException e){
//If name is blank
System.out.println("Please enter a name" + e.getMessage());
}
else if (age == "") {
//Age exists validation
} else if (intAge > 120) {
//Age number validation
} else if (email == "") {
//Email exists validation
} else if (phone == "") {
//Phone exists validation
} else {
//PASSED VALIDATION
fileWrite();
//Ready for next user input
nextEntry();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}