这是我正在开发的屏幕: 问题如下: 如果输入错误的输入,我会得到错误字段的所有错误。然而,对于最后一个字段,它一直告诉我我需要输入一个有效的字段(即使它被纠正)。它只对最后一个字段执行此操作。有谁知道我的问题所在?
这是我的代码:
public void actionPerformed(ActionEvent e) {
try{
//Check if the field of the enterprise name is empty, if it is empty, generate an error message
if(enterpriseGeneral.getEnterpriseName().getText().length() != 0){
this.entName = enterpriseGeneral.getEnterpriseName().getText();
} else{
correct = false;
ErrorMessage = 1;
}
//Check if the field of the first name (admin) is empty, if it is empty, generate an error message
if(userDetail.getFirstName().getText().length() != 0){
this.firstName = userDetail.getFirstName().getText();
} else{
correct = false;
ErrorMessage = 2;
}
//Check if the field of the last name (admin) is empty, if it is empty, generate an error message
if(userDetail.getLastName().getText().length() != 0){
this.lastName = userDetail.getLastName().getText();
} else{
correct = false;
ErrorMessage = 3;
}
//This if-loop checks if the input of the field 'phone number' is a valid phone number (admin), if it's not, it generates an error message
Pattern pattern = Pattern.compile("[0][0-9]{8,12}");
Matcher mat2 = pattern.matcher( userDetail.getPhoneNumber().getText());
if(mat2.matches()){
phoneNumber = Integer.parseInt(userDetail.getPhoneNumber().getText());
} else{
correct = false;
ErrorMessage = 4;
}
//This if-loop checks if the input of the field 'e-mail' is a e-mail address (admin), if it's not, it generates an error message
Pattern pattern2 = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
Matcher mat = pattern2.matcher(userDetail.getEmail().getText());
if(mat.matches()){
this.email = userDetail.getEmail().getText();
} else{
correct = false;
ErrorMessage = 5;
}
//Check if the field of the enterprise street name is empty, if it is empty, generate an error message
if(enterpriseAdres.getStreet().getText().length() != 0){
this.entStreet = enterpriseAdres.getStreet().getText();
} else{
correct = false;
ErrorMessage = 6;
}
//Checks if the street number of the enterprise address is an actual number.
Pattern pattern3 = Pattern.compile("[0-9]+");
Matcher mat3 = pattern3.matcher(enterpriseAdres.getStreetNumber().getText());
if(mat3.matches() ){
entStreetNumber = Integer.parseInt(enterpriseAdres.getStreetNumber().getText());
} else{
correct = false;
ErrorMessage = 7;
}
this.entBus = enterpriseAdres.getBus().getText();
//Check if the field of the enterprise ZIPcode is empty, if it is empty, generate an error message
if(enterpriseAdres.getZipCode().getText().length() != 0){
this.entZip = enterpriseAdres.getZipCode().getText();
} else{
correct = false;
ErrorMessage = 8;
}
//Check if the field of the enterprise city is empty, if it is empty, generate an error message
if(enterpriseAdres.getCityName().getText().length() != 0){
this.entCity = enterpriseAdres.getCityName().getText();
} else{
correct = false;
ErrorMessage = 9;
}
//Check if the field of the enterprise country name is empty, if it is empty, generate an error message
if(enterpriseAdres.getCountry().getText().length() != 0){
this.entCountry = enterpriseAdres.getCountry().getText();
} else{
correct = false;
ErrorMessage = 10;
}
//Check if the field of the admin street name is empty, if it is empty, generate an error message
if(userAdres.getStreet().getText().length() != 0){
this.userStreet = userAdres.getStreet().getText();
} else{
correct = false;
ErrorMessage = 11;
}
//Checks if the street number of the admin address is an actual number.
Pattern pattern4 = Pattern.compile("[0-9]+");
Matcher mat4 = pattern4.matcher(userAdres.getStreetNumber().getText());
if(mat4.matches() ){
userStreetNumber = Integer.parseInt(userAdres.getStreetNumber().getText());
} else{
correct = false;
ErrorMessage = 12;
}
this.userBus = userAdres.getBus().getText();
//Check if the field of the admin ZIPcode is empty, if it is empty, generate an error message
if(userAdres.getZipCode().getText().length() != 0){
this.userZip = userAdres.getZipCode().getText();
} else{
correct = false;
ErrorMessage = 13;
}
//Check if the field of the enterprise name is empty, if it is empty, generate an error message
if(userAdres.getCityName().getText().length() != 0){
this.userCity = userAdres.getCityName().getText();
} else{
correct = false;
ErrorMessage = 14;
}
//Check if the field of the admin country name is empty, if it is empty, generate an error message
if(userAdres.getCountry().getText().length() != 0){
this.userCountry = userAdres.getCountry().getText();
} else{
correct = false;
ErrorMessage = 15;
}
} catch (NumberFormatException e2) {
correct = false;
}
//If all fields are inputed correctly, a new customer is added
if (correct == true){
//Hier komt de DAO insert.
//The login name will be the firstname followed by the first letter of the Last name.
final String loginName = userDetail.getFirstName().getText() + userDetail.getLastName().getText().charAt(0);
JOptionPane.showMessageDialog(null, "User added!\nThe login name for the admin is: " + loginName + "\n The password for user " + loginName + " is: " + randomString() );
}
//If there were some fields that were wrongly submitted, switch-case loops over the errors and generates the error message for the corresponding field
else{
switch(ErrorMessage){
case 1:
JOptionPane.showMessageDialog(null, "Please enter a valid enterprise name");
break;
case 2:
JOptionPane.showMessageDialog(null, "Please enter a valid first name for the admin");
break;
case 3:
JOptionPane.showMessageDialog(null, "Please enter a valid last name for the admin");
break;
case 4:
JOptionPane.showMessageDialog(null, "Please enter a valid phone number for the admin");
break;
case 5:
JOptionPane.showMessageDialog(null, "Please enter a e-mail address for the admin");
break;
case 6:
JOptionPane.showMessageDialog(null, "Please enter a valid street name for the enterprise address");
break;
case 7:
JOptionPane.showMessageDialog(null, "Please enter a valid street number for the enterprise address");
break;
case 8:
JOptionPane.showMessageDialog(null, "Please enter a valid zip code for the enterprise address");
break;
case 9:
JOptionPane.showMessageDialog(null, "Please enter a valid city name for the enterprise address");
break;
case 10:
JOptionPane.showMessageDialog(null, "Please enter a valid country name for the enterprise address");
break;
case 11:
JOptionPane.showMessageDialog(null, "Please enter a valid street name for the admin address");
break;
case 12:
JOptionPane.showMessageDialog(null, "Please enter a valid street number for the admin address");
break;
case 13:
JOptionPane.showMessageDialog(null, "Please enter a valid zip code for the admin address");
break;
case 14:
JOptionPane.showMessageDialog(null, "Please enter a valid city name for the admin address");
break;
case 15:
JOptionPane.showMessageDialog(null, "Please enter a valid country name for the admin address");
break;
}
}
}
例如: 放入除电话和电子邮件之外的所有领域: 它给出了错误的电子邮件通知(我输入了一个好的通知)。
它给出了错误的电话通知(我输入了一个好的通知)。 - >它一直说我需要输入有效的电话号码
答案 0 :(得分:1)
通过" last",我假设你的意思是"国家"。基于你的上一条评论,似乎这些在其他地方被初始化,并且在调用actionPerformed()之后没有被清除,所以在下次调用时,它们与之前的调用相同,如果没有错,它们仍然有错误值从以前的电话。你可能想做一个
correct = true;
位于actionPerformed()
的顶部