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();
}
ifanelse ifand move it outside the firstif.Stream.of(textField1, textField2, ...).map(f -> f.getText()).findAny().isPresent(). Or put the text fields into an array or list and create the stream usingArrays.stream(array)orlist.stream()respectively.