Results 1 to 5 of 5
  1. #1
    xxgetownedxx is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default The operator || is undefined for the argument type(s) boolean, String

    I got this error when I finished typing this line of code.
    The operator || is undefined for the argument type(s) boolean, String.
    Can someone please explain to me what is wrong and how to fix it?

    Java Code:
    if (correct == "Yes"  ||  "yes"){}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: The operator || is undefined for the argument type(s) boolean, String

    First of all, don't ever use == to check equality of Strings. Use the equals(...) method instead.

    The logical OR operator || is inserted between two boolean expressions. Not between a boolean and a String. Which is what the compiler error says.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    79
    Rep Power
    0

    Default Re: The operator || is undefined for the argument type(s) boolean, String

    So more like this ....
    Java Code:
     if(correct.equals("Yes")||correct.equals("yes")){}

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: The operator || is undefined for the argument type(s) boolean, String

    Yep, that's it; operators don't 'distribute' in Java, just as they don't in natural languages; compare this: "do you want a beer or a wine?" with this: "do you want a beer or to die?"; the second phrase makes you feel itchy while both phrases are (more or less) syntactically equivalent. The || operator takes two boolean expressions/values and your first attempt was a boolean expression and a String expression. The compiler didn't like it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: The operator || is undefined for the argument type(s) boolean, String

    <nitpick>
    Use the equalsIgnoreCase method and you don't need the or.
    </nitpick>

Similar Threads

  1. Replies: 7
    Last Post: 03-29-2012, 09:26 AM
  2. Replies: 3
    Last Post: 11-18-2011, 08:15 AM
  3. operator undefined for argument type int, String
    By aconti in forum New To Java
    Replies: 8
    Last Post: 05-30-2011, 07:32 AM
  4. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  5. Replies: 2
    Last Post: 05-07-2010, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •