简体   繁体   English

Java验证循环无法纠正最后的错误

[英]Java validation loop does not correct last mistake

this is the screen I'm developing: 这是我正在开发的屏幕: 忙碌的猫 The problem is the following: If I enter wrong inputs, I do get all the errors of the wrong fields. 问题如下:如果我输入了错误的输入,我会得到所有错误字段的错误。 However, for the last field it keeps telling me that I need to put in a valid one (even if it is corrected). 但是,对于最后一个字段,它总是告诉我我需要输入一个有效的字段(即使已更正)。 It only does this with the last field. 它仅对最后一个字段执行此操作。 Does anyone know where my problem is situated? 有谁知道我的问题所在?

This is my code: 这是我的代码:

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;

                    }
                }


            }

For example: Put in all fields except phone and email: It gives the wrong email notification (i enter a good one). 例如:在电话和电子邮件以外的所有字段中输入:它给出了错误的电子邮件通知(我输入了一个好消息)。

it gives a wrong phonenumber notification(i enter a good one). 它给出了错误的电话号码通知(我输入了一个好电话)。 -> it keeps saying i need to enter a valid phonenumber ->一直说我需要输入一个有效的电话号码

By "last", I'm assuming you mean "Country". “最后”是指您的国家。 Based on your last comment, is seems like these are initialized elsewhere, and are not cleared after the call to actionPerformed(), so on next call, they are the same as previous call, and if nothing is wrong, they still have error value from previous call. 根据您的最后一条评论,似乎这些变量已在其他地方初始化,并且在调用actionPerformed()之后未清除,因此在下一次调用时,它们与上一次调用相同,并且如果没有错误,则它们仍具有错误值来自先前的通话。 You probably want to do a 您可能想做一个

correct = true;

at the top of actionPerformed() 在actionPerformed()的顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM