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? 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...

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.

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



Relevant Pages