0

I have an if statement that validate the input of user. For example, there are 2 things that I would like to validate. First is whether all the textfield is empty. If one of the textfield is no filled in, a pop up appear. Second is whether password and confirm password is the same. When password and confirm password is not the same, a pop up appear. The problem is when both condition met, two pop up appear continuously. What I like to do is when both of the scenario happens, the nested if will not trigger instead only the main if will trigger. The nested if will trigger only when all of the textfield are filled and password and confirm password are not matched.

if(TextField1.getText().isEmpty()||TextField2.getText().isEmpty()||TextField2.getText().isEmpty()
            ||TextField4.getText().isEmpty()||TextField5.getText().isEmpty()||TextField6.getText().isEmpty()
            ||TextField8.getText().isEmpty()||TextField9.getText().isEmpty()||TextField10.getText().isEmpty()){
        JOptionPane.showMessageDialog(null, "Please fill in all the details!",
        "Invalid Input", JOptionPane.ERROR_MESSAGE);
        if(!TextField4.getText().equals(TextField5.getText())){
            JOptionPane.showMessageDialog(null, "Password and Confirm Password is not the same!",
        "Invalid Input", JOptionPane.ERROR_MESSAGE);
        }
    }else{
           writetoFile();
}
3
  • 2
    Make the second if an else if and move it outside the first if. Commented Nov 2, 2020 at 11:29
  • Consider adding the textfields to an array, and iterate through that array to find if any of them needs all the details. docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Nov 2, 2020 at 11:30
  • You could replace all of the empty checks with a stream: Stream.of(textField1, textField2, ...).map(f -> f.getText()).findAny().isPresent(). Or put the text fields into an array or list and create the stream using Arrays.stream(array) or list.stream() respectively. Commented Nov 2, 2020 at 11:38

2 Answers 2

1

You probably need an if-ifelse-else condition here. Try something like this (code untested)

if(TextField1.getText().isEmpty()||TextField2.getText().isEmpty()||TextField2.getText().isEmpty()
            ||TextField4.getText().isEmpty()||TextField5.getText().isEmpty()||TextField6.getText().isEmpty()
            ||TextField8.getText().isEmpty()||TextField9.getText().isEmpty()||TextField10.getText().isEmpty()){
        JOptionPane.showMessageDialog(null, "Please fill in all the details!",
        "Invalid Input", JOptionPane.ERROR_MESSAGE);
} else if(!TextField4.getText().equals(TextField5.getText())){
            JOptionPane.showMessageDialog(null, "Password and Confirm Password is not the same!",
        "Invalid Input", JOptionPane.ERROR_MESSAGE);
    }else{
           writetoFile();
}
Sign up to request clarification or add additional context in comments.

1 Comment

oh yes, I forgot this option.
1

After Triggering the first popup use return; then it wont go to the second validation.

JOptionPane.showMessageDialog(null, "Please fill in all the details!",
    "Invalid Input", JOptionPane.ERROR_MESSAGE);
return;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.