简体   繁体   中英

How do exit a while loop with a JOptionPane

I am having trouble with my code, I am trying to exit this loop if the user clicks on the CANCEL.OPTION or the CLOSED.OPTION . I handled the exception but can't seem to be able to use the buttons on the window. The program gets the users birthyear from their input of their age. The problem i am having is that I cannot end the loop through the buttons. Thanks in advance!

public Integer getBirthYear() {
    boolean prompt = true;
    while(prompt) {
        String enteredAge = showInputDialog(null,"Enter age:");
        try {
            age = Integer.parseInt(enteredAge);
            if(age == JOptionPane.CANCEL_OPTION || age == JOptionPane.CLOSED_OPTION) {
                System.out.println("MADE IT INTO IF");
            }
            age = year - age;
            prompt = false;
            showMessageDialog(null,age);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
    return age;
}

There are several overloaded versions of showInputDialog, with different parameters. Only the javadoc of the last version documents correctly that the return value is null when the user pressed cancel.

public int getBirthYear() {
    boolean prompt = true;
    while (prompt) {
        String enteredAge = showInputDialog(null,"Enter age:");
        if (enteredAge == null) { // Cancel pressed
            age = -1;
            prompt = false;
        } else {
            try {
                age = Integer.parseInt(enteredAge);
                age = year - age;
                prompt = false;
                showMessageDialog(null, age);
            } catch (NumberFormatException e) {
                showMessageDialog(null, "Enter a valid number");
            }
        }
    }
    return age;
}

It might be you want to restructure the program a bit; make age a local variable or whatever. Use int for real integer values. Integer is the Object wrapper class for an int.

The most easiest answer to your question is, just put promp = false outside of your try and catch clause and it should work:

public Integer getBirthYear() {
boolean prompt = true;
while(prompt) {
    String enteredAge = showInputDialog(null,"Enter age:");
    try {
        age = Integer.parseInt(enteredAge);
        if(age == JOptionPane.CANCEL_OPTION || age == JOptionPane.CLOSED_OPTION) {
            System.out.println("MADE IT INTO IF");
        }
        age = year - age;
        showMessageDialog(null,age);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
        prompt = false;
}
return age;
}

The problem was in the scope. The entered age wasnt making it into the catch block. I resolved that issue.

    public Integer getBirthYear() {
    boolean prompt = true;
    do {
        try {
            age = year - Integer.parseInt(showInputDialog(null,"Enter age:"));
            prompt = false;
            showMessageDialog(null, age);
        } catch (NumberFormatException e) {
            String ageEntered = showInputDialog(null,"Enter age:");
            e.printStackTrace();
            if(ageEntered == null) { //close the file if Cancel is chosen
                prompt = false;
            }
        }
    } while(prompt);
    return age;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM