Search code examples
javajoptionpane

Why does my program give the wrong output once you have clicked a button?


In my program I use a JOptionPane.showConfirmDialog to prompt the user if they are sure they want to exit the simulation; if you click "NO" the prgoram should print out "Continuing siumlation..." and continue on with the program however I have found that when you click no it prints out "Continuting simulation" and then straight after "Maintenance will not be carried out, exiting Simulation..." Can anyway help me find out as to why this happens?

            if(main.equalsIgnoreCase("N") | main.equalsIgnoreCase("NO")){
                result = JOptionPane.showConfirmDialog(null, "WARNING! Are you sure you don't want to carry out maintenance? The simulation will stop if you click YES.  ", "TERMINATE THE SIMULATION? ", JOptionPane.YES_NO_OPTION);
                if(JOptionPane.NO_OPTION == result){
                    System.out.println("Continuing Simulation...");
                }
                if (JOptionPane.YES_OPTION == result) {
                    Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
                } 
                if(JOptionPane.YES_OPTION == Result2){
                    System.out.println("Maintenance will not be carried out, exiting Simulation...");
                    try{
                        System.exit(0);
                    }
                    finally{
                        System.err.println("Exiting...");
                        System.exit(1);
                    }
                }
                if (JOptionPane.NO_OPTION == Result2) {
                    System.out.println("Continuing simulation...");
            }
            }

Solution

  • If you click "No" in the first dialog, then you do not assign any value to Result2 because you're not showing the second dialog. However you're still using the value of Result2 in this if-statement if(JOptionPane.YES_OPTION == Result2){.

    The problem is, if Result2 is an instance variable, and you haven't assigned a value to it yet, then it has the default value for the type int, which is zero.

    And the value of the constant JOptionPane.YES_OPTION is also zero. So that if-statement evaluates to true and it prints the maintenance message and exits.

    You should nest the if-statements so that the check for the value of Result2 is only executed when you have assigned a meaningful value to it, like this:

    if (JOptionPane.YES_OPTION == result) {
        Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
        if(JOptionPane.YES_OPTION == Result2){
            System.out.println("Maintenance will not be carried out, exiting Simulation...");
            try{
                System.exit(0);
            }
            finally{
                System.err.println("Exiting...");
                System.exit(1);
            }
        }
        if (JOptionPane.NO_OPTION == Result2) {
            System.out.println("Continuing simulation...");
        }
    }