Search code examples
javaswingjoptionpanemessagedialog

If else branch never used


I'm using if else and writing down some codes suddenly happens the else said the branch is never used... but it should work.

The statement looks like this

JOptionPane.showConfirmDialog(null,full_name+"\n"+number_age+"\n"+gender+"\n"+address+
                 "\n"+birthday+"\n"+cell_no,"CHECK INFORMATION!",JOptionPane.YES_NO_OPTION);
if(true){
    JOptionPane.showMessageDialog(null,"Thank you for Entering");
}else{
    JOptionPane.showMessageDialog(null,"Please Retry");
}

Solution

  • Check JOptionPane.showConfirmDialog response and compare with JOptionPane.YES_NO_OPTION:

    int answer = JOptionPane.showConfirmDialog(null,full_name+"\n"+number_age+"\n"+gender+"\n"+address+
                     "\n"+birthday+"\n"+cell_no,"CHECK INFORMATION!",JOptionPane.YES_NO_OPTION);
    if (JOptionPane.YES_OPTION == answer)) {
        JOptionPane.showMessageDialog(null,"Thank you for Entering");
    } else {
        JOptionPane.showMessageDialog(null,"Please Retry");
    }