Reputation: 431
This is working for "draw" but not for discard". I just want it to validate whether the input is one of the two. I run into this problem frequently and don't understand why. Does anyone see the problem?
while(!(choice.equalsIgnoreCase("draw") && !(choice.equalsIgnoreCase("discard"))))
Thanks.
Upvotes: 0
Views: 47
Reputation: 106500
Simplify it (and watch your parentheses in the future):
while(choice.equalsIgnoreCase("draw") || choice.equalsIgnoreCase("discard")) {
// code
}
What your code is currently doing is this:
!(choice.equalsIgnoreCase("draw") && !(choice.equalsIgnoreCase("discard")))
Pay close attention to that negation. It's closing the entire expression, because you've opened up the paren too wide. That would evaluate to true
only if you entered in "discard"
.
If you meant to say this:
!choice.equalsIgnoreCase("draw") && !choice.equalsIgnoreCase("discard"))
Then that contradicts what you're trying to evaluate - you want to check to see if one of those evaluates to true
. If you correct the parentheses, then it would still be incorrect, as it only evaluates if both of those statements are false
.
Upvotes: 0
Reputation: 201507
You forgot to close a parenthesis for the "draw" test, you could use
while (!(choice.equalsIgnoreCase("draw")) &&
!(choice.equalsIgnoreCase("discard")))
or
while (!choice.equalsIgnoreCase("draw") &&
!choice.equalsIgnoreCase("discard"))
or use De Morgan's Laws like
while (!(choice.equalsIgnoreCase("draw") ||
choice.equalsIgnoreCase("discard")))
Upvotes: 1
Reputation: 262
If you're trying to check if the input is one of the two, you want to do
while(choice.equalsIgnoreCase("draw") || choice.equalsIgnoreCase("discard"))
Right now you're only entering the loop when choice
is neither "draw"
nor "discard"
Upvotes: 1