0

I'm trying to do a "do while" loop with a nested if statement. I'm trying to compare two possible values for a String variable "word". If !word.equals "deeppan or thin" do something, else do something. But its not liking me using the or || comparator .. Any suggestions would be welcome.

 do {
        word = scan.next();
        if ( !word.equalsIgnoreCase( "Deeppan" || "thin" ) ) {
            System.out.print("Sorry you must specify a Deeppan or thin base, try again: ");
        } else {
            break;
        }
    } while ( true );
3
  • 6
    You misunderstand the || operator. Commented Dec 10, 2013 at 16:21
  • 2
    "Any suggestions would be welcome." Always copy/paste error & exception output. Commented Dec 10, 2013 at 16:22
  • Programming isn't like making sentences: One would say: Foo doesn't equal X or Y. While the Java compiler only understands: Foo doesn't equal X and Foo doesn't equal Y. Commented Dec 10, 2013 at 16:24

5 Answers 5

6

equalsIgnoreCase takes a single string argument, not a logical expression. You can combine them with || or && though:

if (!word.equalsIgnoreCase( "Deeppan") && !word.equalsIgnoreCase("thin" ))
Sign up to request clarification or add additional context in comments.

2 Comments

Actually he's not passing more than one argument.. Also I think the ! should be outside, refer to De-Morgan laws.
This is wrong. Your boolean logic is not correct: if (!((word.equalsIgnoreCase( "Deeppan") || word.equalsIgnoreCase("thin")))){ is more like it.
3

You have to do it like this:

if (!word.equalsIgnoreCase("Deeppan") && !word.equalsIgnoreCase("thin")) {

Think about the || which i switched to &&, because the if should only be true, if the value is not the first AND not the second one!

4 Comments

Why downvoting? All answers of you are wrong with the logical OR, aren't they?
You got my +1 dude :)
@Downvoter have you ever heard of De Morgan's laws? In short !(a | b) == !a & !b.
For the fans of OR logic, if (!(word.equalsIgnoreCase("Deeppan") || word.equalsIgnoreCase("thin"))) {
1

This part is wrong, that's not how you use the boolean || operator, and anyway the logic is incorrect:

if (!word.equalsIgnoreCase("Deeppan" || "thin"))

It should be like this, comparison-operator-comparison, and notice the correct way to state the comparison for the effect you want to achieve:

if (!(word.equalsIgnoreCase("Deeppan") || word.equalsIgnoreCase("thin")))

Or equivalently, using De Morgan's laws (and easier to read and understand, IMHO):

if (!word.equalsIgnoreCase("Deeppan") && !word.equalsIgnoreCase("thin"))

5 Comments

@MartijnCourteaux I don't think so... how should it be, then?
Well, yeah, I see. You are applying right, but you started wrong by saying that what he wants is (!x || !y), instead of (!x && !y).
It seems that OP is trying to achieve !(word.equalsIgnoreCase("Deeppan") || word.equalsIgnoreCase("thin")) which should be !word.equalsIgnoreCase("Deeppan") && !word.equalsIgnoreCase("thin")
Change it with || instead of &&. A word can not be equal to both "deeppan" and "thin". Instead you want the condition to be "not equal to (deeppan or thin)"
You're right guys, I didn't stop to think if the logic in the question was correct in the first place. I updated my answer
0

You have a few issues going on. First:

 "Deeppan" || "thin"

is attempting to use the boolean "OR" operator to compare two strings. The "OR" operator can only compare boolean results and returns a boolean that is the result of the comparison:

      System.currentTimeMillis() == 123455667 || object.equals(this) // both sides are boolean results. 
      true || false  // returns 'false' 

But let's pretend for a second that "Deeppan" || "thin" is OK (remember, it isn't) and the compiler knows that you want to compare the two strings. It still leaves the issue that the OR operator returns a boolean result (true or false), which you are then attempting to pass into the method equalsIgnoreCase on the word variable. equalsIgnoreCase takes a String argument, not a boolean. This is the second compilation issue. As has been pointed out, what you need is to check for the conditions separately and OR the result to get the final boolean

if("Deeppan".equalsIgnoreCase(word) || "thin".equalsIgnoreCase(word)) {
   // do something
}

Comments

-2
("Deeppan" || "thin")

is a boolean expression. equalisIgnoreCase takes a string. Therefore you need to make two seperate calls and OR the (boolean) results

1 Comment

Downvoted because it's not any kind of expression. It's illegal syntax to apply the || operator to a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.