gpt4 book ai didi

Java 验证循环未纠正最后一个错误

转载 作者:行者123 更新时间:2023-11-30 07:47:17 25 4
gpt4 key购买 nike

这是我正在开发的屏幕: a busy cat问题如下:如果我输入错误的内容,我确实会得到错误字段的所有错误。然而,对于最后一个字段,它不断告诉我需要输入一个有效的字段(即使它已被更正)。它仅对最后一个字段执行此操作。有谁知道我的问题出在哪里?

这是我的代码:

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;

}
}


}

例如:输入除电话和电子邮件之外的所有字段:它给出了错误的电子邮件通知(我输入了一个正确的电子邮件通知)。

它给出了错误的电话号码通知(我输入了一个好的电话号码)。-> 它一直说我需要输入有效的电话号码

最佳答案

我假设您所说的“最后”是指“国家/地区”。根据您的最后评论,这些似乎是在其他地方初始化的,并且在调用 actionPerformed() 后没有清除,因此在下一次调用时,它们与上一次调用相同,如果没有任何问题,它们仍然有错误值从之前的通话中。您可能想做一个

correct = true;

在actionPerformed()的顶部

关于Java 验证循环未纠正最后一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com