Connecting Tech Pros Worldwide Help | Site Map

Simple syntax question

  #1  
Old February 28th, 2008, 11:15 PM
somebody
Guest
 
Posts: n/a
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("one")) && !(str.equals("two")) &&
!(str.equals("three")) && !(str.equals("four")) &&
(mystr.compareTo("X") != 0) )
{

Then do this...
  #2  
Old February 29th, 2008, 10:06 AM
Robert Larsen
Guest
 
Posts: n/a

re: Simple syntax question


somebody wrote:
Quote:
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("one")) && !(str.equals("two")) &&
!(str.equals("three")) && !(str.equals("four")) &&
(mystr.compareTo("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)|(three)|(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
  #3  
Old April 6th, 2008, 10:35 AM
Frank Stallone
Guest
 
Posts: n/a

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:
Quote:
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("one")) && !(str.equals("two")) &&
!(str.equals("three")) && !(str.equals("four")) &&
(mystr.compareTo("X") != 0) )
{
>
Then do this...
spammer -enquiries@optimaloptimization.com
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple syntax question Arthur answers 0 November 22nd, 2005 01:36 AM
simple syntax question Arthur answers 0 November 22nd, 2005 01:36 AM
A simple syntax question Greg Smith answers 5 November 18th, 2005 04:20 AM
Simple syntax question(MULTIPOST) Trimbitas Sorin answers 2 July 19th, 2005 05:55 AM
simple syntax question Arthur answers 0 July 19th, 2005 04:19 AM