0

I am trying to create a method that spits out a boolean value if the user enter 1 word that is included in a list of 6. otherwise prompt the user until he enters 1 corrected one

I've tried using a while loop with switch but it doesnt seem to work

  /**
     * If the user says yes or y or true, return boolean value of true
     * if the user says no or n or false, return boolean value of false
     * Display a prompt if user fails to write any of 6 above words until
     * the user does
     */
    public static boolean promptForYesNo(Scanner in, String prompt){
        boolean toReturn;
        boolean valid;
        String userAnswer;
        System.out.println(prompt + "Yes or No?");   
        userAnswer = in.next();
        userAnswer = userAnswer.toLowerCase();
        while (userAnswer.equals("yes") || userAnswer.equals("y") || 
               userAnswer.equals("true") ||userAnswer.equals("no") || 
               userAnswer.equals("n") || userAnswer.equals("false")){
            if (userAnswer.equals("yes") || userAnswer.equals("y") || 
                userAnswer.equals("true")){
                     toReturn = true;
            }
            else if(userAnswer.equals("no") || userAnswer.equals("n") || 
                    userAnswer.equals("false")){
                     toReturn = false;
            }
            else {
                System.out.println(prompt + "Yes or No?");   
                userAnswer = in.next();
                userAnswer = userAnswer.toLowerCase();
            }
        }
        return toReturn;
    }
forpas
  • 160,666
  • 10
  • 38
  • 76

2 Answers2

1

The above code goes in continuous loop: use break when condition is satisfied

while (conditions) {
        if (conditions) {
            toReturn = true;
            break;
        } else if (conditions) {
            toReturn = false;
            break;
        } else {
            System.out.println(prompt + "Yes or No?");
            userAnswer = in.next();
            userAnswer = userAnswer.toLowerCase();
        }
    }
Shriraj
  • 56
  • 4
1

Even if you initialize the variable, it won't satisfy your intention. Instead, you should recursively call the method again with the arguments until you expect the user's intention, like so:

 public static boolean promptForYesNo(Scanner in, String prompt){
            System.out.println(prompt + "Yes or No?");   
            String userAnswer = in.next();
            userAnswer = userAnswer.toLowerCase();
            if (userAnswer.equals("yes") || userAnswer.equals("y") || 
                    userAnswer.equals("true")){
                return true;
            }
            else if(userAnswer.equals("no") || userAnswer.equals("n") || 
                    userAnswer.equals("false")){
                return false;
            }
            else {
                return promptForYesNo(in, prompt);
            }
        }

I have cleaned up your code a bit.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Prashanth
  • 119
  • 7
  • dude you're a genius. I just started Java so I'm pretty dumb. I wasnt able to grasp the key point inside of your code : return promptForYesNo(in, prompt) . I knew I had to return some kind of boolean value in order for the method to be usable , I kept thinking of having to tweak it in a way of somehow returning the toReturn variable I wrote inside of my own code. Could you please further elaborate the idea of how return promptForYesNo(in, prompt) was possible ? thank u so much dude – Đằng Long Mar 25 '19 at 04:53
  • if `userAnswer` doesn't meet the `if` and `else-if` condition, it will return to `promptForYesNo` function. – John Joe Mar 26 '19 at 07:57
  • we are calling the function promptForYesNo until the user entering the expected value. Check this https://introcs.cs.princeton.edu/java/23recursion/ to learn more. – Prashanth Mar 27 '19 at 06:13