Comparison of if-else not continuing even with else in Java

0

I have two variables:

int trabalhadorFormal;
int dispensadoIndireta;

And with them I'm using JOptionPane.showConfirmDialog to confirm as a kind of boolean, yes or no, for the user.

trabalhadorFormal = JOptionPane.showConfirmDialog(null, 
    "É trabalhador formal?",
    "Por favor selecione",
    JOptionPane.YES_NO_OPTION);

dispensadoIndireta = JOptionPane.showConfirmDialog(null, 
    "Foi dispensado indiretamente?",
    "Por favor selecione",
    JOptionPane.YES_NO_OPTION);

And with this I have the following code:

 if (trabalhadorFormal == JOptionPane.YES_OPTION) {
     if (dispensadoIndireta == JOptionPane.NO_OPTION) {
         do {
             opcao = Integer.parseInt(JOptionPane.showInputDialog("[1] - É empregado doméstico" + 
                 "\n[2] - Não é empregado doméstico"));

             switch (opcao) {
                 case 1:
                     JOptionPane.showMessageDialog(null, "É empregado doméstico!");
                     break;
                 case 2:
                     JOptionPane.showMessageDialog(null, "Não é empregado doméstico!");
                     break;
                 default:
                     JOptionPane.showMessageDialog(null, "Opção inválida!");
                     break;
             }
         } while (opcao != 1 && opcao != 2);           
     } else {
         JOptionPane.showMessageDialog(null, "Teste!");
     }
 }

The only problem is that when I select the options that will take me to else after the second if , nothing happens and the program closes me returning successfully, just by ignoring else .

What is going wrong here? In the construction of if-else ?

    
asked by anonymous 06.03.2018 / 22:12

1 answer

2

When you enter with the option not for dispensaIndirect in the second if it exits the else condition, then else will not be processed, look at this example:

public static void main(String[] args) {
        boolean condicao = true;
        int opcao = 2;

        if (condicao == true) {
            switch (opcao) {
            case 1:
                System.out.println("Opcao 1");
                break;
            case 2:
                System.out.println("Opcao 2");
                break;
            case 3:
                System.out.println("Opcao 3");
                break;
            }
        } else {
            System.out.println("Para condicao false entra aqui!");
        }
    }

In the above case, print Option 2 and do nothing else, for I have set condition condition and if > will not be called anymore,     

06.03.2018 / 23:30