0

I was completing a high school java project, but I stumbled across this issue that I can't seem to fix no matter what I do.

do  //runs until user enters a valid option
            {
                understood = input.next();  //collects user input 
                if (!understood.equalsIgnoreCase("yes") || !understood.equalsIgnoreCase("y") || !understood.equalsIgnoreCase("no") || !understood.equalsIgnoreCase("n"))
                                {
                    System.out.print("\nHey, man. Answer the question. Yes or no? ");
                }
            }
            while (!understood.equalsIgnoreCase("yes") || !understood.equalsIgnoreCase("y") || !understood.equalsIgnoreCase("no") || !understood.equalsIgnoreCase("n"));

What I'm trying to do is check for invalid input for a string in a yes or no question, but when I run it, no matter what I enter, it is treated as invalid input. I'd really appreciate any help on this, thank you so much! :)

beebs
  • 1
  • 1
    Aside from reason mentioned in duplicate (linked at top of your question, you may need to refresh this page to see it) you can also improve your code a little since currently you are validating same data twice: once in `if(invalidChoice)` and once in `while(invalidChoice)`. So instead of `do { choice = ...; if (invalidChoice){ problem info;} } while (invalidChoice);` you could rewrite it into something IMO more readable like `choice = ...; while(invalidChoice){problem info; choice = ...:}`. – Pshemo Jan 08 '22 at 00:59

1 Answers1

0

It can be resolved by changing those OR statements (||) and adding AND (&&) statements in the place

do {
    understood = input.next();

    if (!understood.equalsIgnoreCase("yes") && !understood.equalsIgnoreCase("y") && !understood.equalsIgnoreCase("no") && !understood.equalsIgnoreCase("n")) {
      System.out.print("\nHey, man. Answer the question. Yes or no? ");
    }
} while (!understood.equalsIgnoreCase("yes") && !understood.equalsIgnoreCase("y") && !understood.equalsIgnoreCase("no") && !understood.equalsIgnoreCase("n"));