I'm getting a operator undefined error when I use ||

Ulrich Batanado :

I'm trying to get the code to check if an input either has nothing and is entered or doesn't have either Y or N, but I keep getting an error.

I've tried using just " | " but that doesn't work, and " , " just doesn't make sense.

while (answerToFirstQuestionYN.isEmpty() && !answerToFirstQuestionYN.equalsIgnoreCase("Y" || "N")) {
            System.out.println("Input not recognized, try again.");
            answerToFirstQuestionYN = reader.next();
        }

The error message is " The operator "||" is undefined for the argument type(s) java.lang.String, java.lang.String "

Mark Rotteveel :

The || operator can only join boolean expressions. So you need to separate the conditions to:

!(answerToFirstQuestionYN.equalsIgnoreCase("Y") ||
  answerToFirstQuestionYN.equalsIgnoreCase("N"))

Your first condition (answerToFirstQuestionYN.isEmpty()) needs to be removed, otherwise the whole condition is only true if the string is empty.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=309953&siteId=1