Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Logical OR Operator and Strings

    I have a project for school, and I'm having trouble with one section. I read in a string and compare it to a regular expression. When it matches i need to discard some entries. As an example. The String will be ("IF (x = y) THEN"). The regular expression is supposed to be able to read all identifiers.
    an Identifier is described as any string starting with a letter and then containing any number of characters or digits.

    So it works in that the list I create will find the IF,x,y,THEN.

    So what I want to do is to test to see it the match is NOT IF or THEN.

    here is the code that doesn't work...
    while (m.find()){

    if ((!m.group(0).equals("IF")) || (!m.group(0).equals("THEN")))
    {
    System.out.println("true : " + m.group(0));
    }

    If I change it to

    while (m.find()){

    if (!m.group(0).equals("IF"))
    {
    System.out.println("true : " + m.group(0));
    }

    it works fine and only continues if the m.group(0).equals is NOT if. So I don't seem to understand why since they can't both be true at the same time, and I have tried every combination of brackets and operators. I know I can make it one big nested if statement, but that seems bulky, and I think this should work.

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

    Default Re: Logical OR Operator and Strings

    Quote Originally Posted by kbreitsprecher View Post
    if ((!m.group(0).equals("IF")) || (!m.group(0).equals("THEN")))
    This line is not correct: if the String equals "IF" it doesn't equal "THEN" so the if condition will be true; alternatively if the String equals "THEN" it won't be equal nnto "IF", again making that entire test true. Change the || operator to the && operator.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Logical OR Operator and Strings

    Update:

    I'm still confuse as to why the above doesn't work, but when I make it a positive expression

    if ((!m.group(0).equals("IF")) || (!m.group(0).equals("THEN")))
    {
    }
    else
    {
    System.out.println("true : " + m.group(0));
    }

    It then works as I expected it. Is there something about the Logical OR i'm not quite understanding.

  4. #4
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Logical OR Operator and Strings

    Quote Originally Posted by JosAH View Post
    This line is not correct: if the String equals "IF" it doesn't equal "THEN" so the if condition will be true; alternatively if the String equals "THEN" it won't be equal nnto "IF", again making that entire test true. Change the || operator to the && operator.

    kind regards,

    Jos
    OK, I see why, I was assuming that if either was False or should handle it, but I really need the AND... Thanks

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

    Default Re: Logical OR Operator and Strings

    Quote Originally Posted by kbreitsprecher View Post
    OK, I see why, I was assuming that if either was False or should handle it, but I really need the AND... Thanks
    You have a double negation in your expression, i.e. !A && !B; maybe that expression is a bit clearer when written as !(A || B); in words it reads: not (IF or THEN)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. >>> and >> Logical operators
    By Playagood in forum New To Java
    Replies: 3
    Last Post: 02-19-2012, 05:07 AM
  2. Replies: 5
    Last Post: 01-22-2012, 12:02 AM
  3. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 01:29 PM
  4. Logical Operator query
    By aconti in forum New To Java
    Replies: 5
    Last Post: 08-19-2011, 11:38 AM

Posting Permissions

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