Ryan H
Ryan H

Reputation: 85

Do while loop comparing Strings

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 );

Upvotes: 0

Views: 768

Answers (5)

Konstantin
Konstantin

Reputation: 3294

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

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

Upvotes: 6

MadConan
MadConan

Reputation: 3767

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
}

Upvotes: 0

Óscar López
Óscar López

Reputation: 235994

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"))

Upvotes: 1

pauljwilliams
pauljwilliams

Reputation: 19225

("Deeppan" || "thin")

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

Upvotes: -2

bobbel
bobbel

Reputation: 3357

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!

Upvotes: 3

Related Questions