简体   繁体   中英

else statement proceeds to execute even if statements are encountered

I am working in Java and I have come across a trouble in my code where I have multiple if statements and one else . Despite the if statements being true, the else statements still execute. I've been encountering this all day and I can't seem to find the error. If anyone can help

private void confirmActionPerformed(java.awt.event.ActionEvent evt) {                                       

    if(lname1.getText().equals("")){
         lname_ver1.setText("Field is empty!");

     }
    if (mname1.getText().equals("")){
        mname_ver1.setText("Field is empty!");

    }
    if(fname1.getText().equals("")){
       fname_ver.setText("Field is empty!");

    }
     if(s_age.getSelectedIndex()== 0){
     age_ver.setText("Field is empty!");
    }
    if(!female_button.isSelected()&& !male_button.isSelected()){
    gender_ver1.setText("Select gender!");
    }
    if(month.getSelectedIndex()==0 && day.getSelectedIndex()==0 && year.getSelectedIndex()==0){
            birth_ver1.setText("Field is empty!");
            }

    if (s_num.getText().equals("")){
          num_ver.setText("Field is empty!"); 

    }


     if(!email.getText().matches("\"^[a-zA-Z0-9]+[@]+[a-zA-Z0-9]+[.]+[a-zA-Z0-9]+$")){
       email_ver1.setText("Invalid email!");
     }
     if(email.getText().equals("")){
        email_ver1.setText("Field is empty!");

     if(fathers_name.getText().equals("")){
        father_ver1.setText("Field is empty!")
    }
     if(mothers_name.getText().equals("")){
     mother_ver1.setText("Field is empty!");   
    }
     if(parent_no.getText().equals("")){
     no_verify1.setText("Field is empty!");  
     }
     if(parent_no.getText().contains("[a-zA-Z]")){
         no_verify1.setText("Field is empty!");
     }

     if (s_nat.getText().equals("")){
      nat_ver.setText("Field is empty!");
     }
     if(!single.isSelected() && married.isSelected()){
            nat_ver.setText("Select status!!");
     }



   else
    {
        JOptionPane.showMessageDialog(null, "SUCCESS", "INFO    ", JOptionPane.INFORMATION_MESSAGE);
        dispose();
        Schedule sched = new Schedule();
        sched.setLocationRelativeTo(null);
        sched.setResizable(false);
        sched.setVisible(true);
    }`

1)Please verify whether braces opening and closing is perfectly matching your requirements. 2)If else format.... if{} else if{} else if{} else{} In this format if any of the condition before else works, then else wont execute, but your case is little different like

if{}
if{}
if{}
if{}
.
.
.
.
if{}  ----//last if
else{}

so in this case, else is applicable only for the last if, so when last if fails, it goes for else, and the if conditions which are mentioned before that doesn't have any relation with else.

All the if statements are independent from each other. The else belongs only to the last if statement. It will be executed if single or married have been selected. The other conditions do not matter.

I think, your intention is evaluating user inputs. You want to put an error message to each invalid input and proceed only if everything is valid. If this is what you wanted, there is a simple solution for it:

private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
  boolean valid = true;

  if (lname1.getText().equals("")) {
    lname_ver1.setText("Field is empty!");
    valid = false;
  }
  if (mname1.getText().equals("")) {
    mname_ver1.setText("Field is empty!");
    valid = false;
  }
  ...
  if (valid) {
    JOptionPane.showMessageDialog(null, "SUCCESS", "INFO    ", JOptionPane.INFORMATION_MESSAGE);
    dispose();
    Schedule sched = new Schedule();
    sched.setLocationRelativeTo(null);
    sched.setResizable(false);
    sched.setVisible(true);
  }
}

And keep you parantheses balanced, look at the statement if(email.getText().equals("")){ .

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