【问题标题】:Java validation loop does not correct last mistakeJava 验证循环没有纠正最后一个错误
【发布时间】:2016-02-17 21:41:02
【问题描述】:

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

这是我的代码:

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;

                    }
                }


            }

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

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

【问题讨论】:

  • 首先将字段和条目分成单独的对象怎么样?一个类处理一个条目。在您当前的实现中,仅报告编号最高的不匹配,因为ErrorMessage(应该是errorMessage)被覆盖。您应该允许多个条目不正确。
  • 我确实得到了所有的不匹配。并在输入有效值后全部消失。但是对于最后一个不匹配,在我将其放入有效值后似乎并没有更正它。
  • 您的解决方案不能扩展(我会说它甚至不能很好地缩小)并且设计得不好。此外,它将链接的错误消息和字段分布在各处。

标签: java validation loops switch-statement


【解决方案1】:

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

correct = true;

在actionPerformed()的顶部

【讨论】:

  • 我编辑了问题,检查代码后的场景。是的,我忘了提到变量 private int ErrorMessage = 0;私人布尔正确=真; , 都在顶部声明
  • 那你应该把actionPerformed()开头的correct重置为true
  • 它工作了@rslemos。非常感谢你,问题是我没有在 actionPerformed() 下声明布尔值,而是在它之上!多么愚蠢的错误让我毛骨悚然!谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 2016-06-24
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
相关资源
最近更新 更多