简体   繁体   中英

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. You probably want to do a

correct = true;

at the top of actionPerformed()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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