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