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 / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

Simple syntax question

Thread view: 
somebody - 29 Feb 2008 00: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?  I can't seem to
find anything like a str.unequal.  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...

-Thanks
Knute Johnson - 29 Feb 2008 01:17 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?  I can't seem to
[quoted text clipped - 9 lines]
>
> -Thanks

Ugly isn't it?  You don't need the parenthesis around the
String.equals() to protect the !, you can just write !String.equals().

Also remember that !a && !b == !(a || b).

Signature

Knute Johnson
email s/nospam/knute/

     ------->>>>>>http://www.NewsDem

Joshua Cranmer - 29 Feb 2008 01:57 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?  I can't seem to
[quoted text clipped - 7 lines]
>
>    Then do this...

Long, and not very pretty. Alternatively, you could use something like this:

List<String> strings = Arrays.asList("one", "two", "three", "four");
if (!strings.contains(str) && !mystr.equals("Y")) {

This is also has the benefit of being easier to expand and refactor.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Roedy Green - 01 Mar 2008 02:21 GMT
On Fri, 29 Feb 2008 01:57:00 GMT, Joshua Cranmer
<Pidgeot18@verizon.invalid> wrote, quoted or indirectly quoted someone
who said :

>List<String> strings = Arrays.asList("one", "two", "three", "four");

normal this part would be static final so you build the list only
once.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 29 Feb 2008 03:03 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?  I can't seem to
>find anything like a str.unequal.  I want the if block to execute if str
>equals "seven" and mystr equals "Y"

see http://mindprod.com/jgloss/booleannot.html

you have to say

if (! a.equal( b) )

Intellij Idea has a code simplifier that will take complicated boolean
expressions and convert them to various altervatives using the de
Morgan laws.

see http://mindprod.com/jgloss/intellij.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.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.