Simple syntax question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • somebody

    Simple syntax question

    Is this the best we to conditionally do something if str is NOT equal to
    one, two, three, or four, AND mystr is NOT equal to X?
    For example, I want the if block to execute if str equals "seven" and
    mystr equals "Y"


    if ( !(str.equals("o ne")) && !(str.equals("t wo")) &&
    !(str.equals("t hree")) && !(str.equals("f our")) &&
    (mystr.compareT o("X") != 0) )
    {

    Then do this...
  • Robert Larsen

    #2
    Re: Simple syntax question

    somebody wrote:
    Is this the best we to conditionally do something if str is NOT equal to
    one, two, three, or four, AND mystr is NOT equal to X?
    For example, I want the if block to execute if str equals "seven" and
    mystr equals "Y"
    >
    >
    if ( !(str.equals("o ne")) && !(str.equals("t wo")) &&
    !(str.equals("t hree")) && !(str.equals("f our")) &&
    (mystr.compareT o("X") != 0) )
    {
    >
    Then do this...
    That depends on what you mean by 'best'. It'll work but this may be more
    readable:

    if (!str.matches(" (one)|(two)|(th ree)|(four)") &&
    mystr.compareTo ("X") != 0) {
    //do stuff
    }

    But maybe your way is faster.
    I usually prefer the more readable code to the fastest.


    Best,
    Robert

    Comment

    • Frank Stallone

      #3
      Re: Simple syntax question

      Why not use or as suggested by Robert?

      Maybe I need to wake up a little more but if you want str to equal
      "seven" then why not just test for that?


      On Thu, 28 Feb 2008 18:09:33 -0500, somebody wrote:
      Is this the best we to conditionally do something if str is NOT equal to
      one, two, three, or four, AND mystr is NOT equal to X? For example, I
      want the if block to execute if str equals "seven" and mystr equals "Y"
      >
      >
      if ( !(str.equals("o ne")) && !(str.equals("t wo")) &&
      !(str.equals("t hree")) && !(str.equals("f our")) &&
      (mystr.compareT o("X") != 0) )
      {
      >
      Then do this...
      spammer -enquiries@optim aloptimization. com

      Comment

      Working...