Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Simple syntax question

Thread view: 
somebody - 28 Feb 2008 23:09 GMT
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...
Robert Larsen - 29 Feb 2008 09:58 GMT
> 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?
[quoted text clipped - 7 lines]
>
>    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
Frank Stallone - 06 Apr 2008 10:30 GMT
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?

> 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
[quoted text clipped - 6 lines]
>
>    Then do this...

spammer -> enquiries@optimaloptimization.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.