Home > other >  How do I use boolean method correctly if I need two options?
How do I use boolean method correctly if I need two options?

Time:03-11

The question is, if I need to chose only from two options in boolean method (Yes or No) how do I put it in IFs? I try to do like this (see below), it underlines very last brace. If I use default return outside while (but I don't want to), it underlines first return (after first if).

static boolean isAnotherGamer() {
    System.out.println("Play another game? Type in Y or N");
    Scanner scanner = new Scanner(System.in);
    String answer = scanner.nextLine();
    while (true) {
        if (answer.equalsIgnoreCase("Y")) {
            break;
            return true;
        } else if (answer.equalsIgnoreCase("N")) {
            break;
            return false;
        }
        System.out.println("Input mismatch");
    } //IDE underline this brace
}

CodePudding user response:

Why can you not validate the input first, and then after the input is either a yes or no, decide on what to do. If it is not either, you can make the repetition statement continue to run until after you get what you need. The location of your return statement is the problem because if either if or else if statements are not true, the method will not return a boolean as your method signature suggests, and your method will just be an infinite loop.

CodePudding user response:

Your method is declared to return a boolean. There is no return statement in the flow.

Assume you go into the endless loop. At this moment we evaluate what the user entered (why do we do that inside the endless loop? The answer does not change inbetween, does it?)

If it is 'y', we break the loop. If it is 'n', we break the loop. In any other case we print something and remain in the loop.

But as soon as the loop was broken -> where is the return statement?

So from my POV, the function should look like this:

static boolean isAnotherGamer() {
    Scanner scanner = new Scanner(System.in);

    while (true) {
        System.out.println("Play another game? Type in Y or N");
        String answer = scanner.nextLine();
        if (answer.equalsIgnoreCase("Y")) {
            return true;
        } else if (answer.equalsIgnoreCase("N")) {
            return false;
        }
        System.out.println("Input mismatch");
    }
}

CodePudding user response:

Because you've not set a default return value, if the user doesn't choose either "Y" or "N" then nothing is going to be returned so that's why you're getting an error.

Additionally, you shouldn't be putting any code after your break statements as those lines will be completely ignored (again, nothing returned as your return statements are after your breaks.)

You can just completely remove those break statements if you're just wanting to quit that method once you've got your boolean value or you can update a boolean variable for future use if you're wanting to keep running code inside your method. (I've provided an example of this)

        System.out.println("Play another game? Type in Y or N");
        Scanner scanner = new Scanner(System.in);
        String answer = scanner.nextLine();

        //To store the state of the user's answer
        boolean providedAnswer = false;

        //if the answer was yes, set the boolean's val to true
        if(answer.equalsIgnoreCase("Yes")){
            providedAnswer = true;
        }

        //output the boolean's value
        System.out.println("User wanted to play again? "   providedAnswer);

        //return the boolean value
        return providedAnswer;
    }```

CodePudding user response:

Here is how I would do it. This allows any part of yes or no to be entered. I think it best to pass a Scanner instance rather than creating one each time. Using a regular expression allows for some latitude in the answer.

  • ^$ - beginning and end of string.
  • (?i) - ignore case
  • ye?s? - says must have y but e and s are optional.
static boolean isAnotherGamer(Scanner scanner) {
    System.out.println("Play another game? Type in Y(es) or N(o)");
    while (true) {
        String input = scanner.nextLine();
        if (input.matches("(?i)^ye?s?$")) {
            return true;
        }
        if (input.matches("(?i)^no?$")) {
            return false;
        }
        System.out.println("Incorrect response, please enter Y(es) or N(o)");
    }       
}
  • Related