William Scribner
William Scribner

Reputation: 11

Java: Alert windows will not open under "if" statement

I'm new here and to the programming world in general so please do not hate me.

My class just got into using "if" statements and for some reason I cannot get my javafx Alert boxes to display. Also open to any tips for making my code shorter.

public void start(Stage primarystage) {

    TextInputDialog getDemNums = new TextInputDialog();
    getDemNums.setTitle("How Many?");
    getDemNums.setHeaderText("Input number of people desired: ");
    Optional<String> sNumber = getDemNums.showAndWait();
    int result = Integer.valueOf(sNumber.get());

    if(result>=10){
        int bigGroup = result/2;
        TextInputDialog tenPlus = new TextInputDialog();
        tenPlus.setTitle("Group Assignment");
        tenPlus.setContentText("The group size is " + bigGroup + " people.");
    } else if (result>=3 && result<10) {
        int medGroup = result/3;
        Alert medWindow = new Alert(AlertType.INFORMATION);
        medWindow.setTitle("Group Assignment");
        medWindow.setContentText("The group size is " + medGroup + " people.");
    } else if(result<3) {
        Alert tooSmall = new Alert(AlertType.INFORMATION);
        tooSmall.setTitle("ERROR");
        tooSmall.setContentText("The number of people has to be at least three");

Upvotes: 0

Views: 193

Answers (1)

fabian
fabian

Reputation: 82461

You never do anything with the dialogs you create inside the if or else blocks. Probably you planed to show them...

Furthermore !(result >= 10) is equivalent to result < 10 and !(result >= 3) is equivalent to result < 3 so you don't have to test those conditions again:

Dialog dialog;
if(result>=10){
    int bigGroup = result/2;
    dialog = new TextInputDialog();
    dialog.setTitle("Group Assignment");
    dialog.setContentText("The group size is " + bigGroup + " people.");
} else if (result>=3) {
    int medGroup = result/3;
    dialog = new Alert(AlertType.INFORMATION);
    dialog.setTitle("Group Assignment");
    dialog.setContentText("The group size is " + medGroup + " people.");
} else {
    dialog = new Alert(AlertType.INFORMATION);
    dialog.setTitle("ERROR");
    dialog.setContentText("The number of people has to be at least three");
}
dialog.show(); // or showAndWait()

Upvotes: 2

Related Questions