Way to express a 'not equals' condition

I have searched through the forum and have not found the best way to set up a 'not equals' condition on a 'Print when expression' in iReport (1.3). I have found plenty of samples of an 'equals' condition though.

I have a number of fields in my reports that are flags that indicate whether a user has met a condition or not ('Y' or 'N'). When the flag is 'Y', then I want to display a checkbox with a check in it. When the flag is 'N', then I will display an empty checkbox.

The general consensus I've seen is that for STRINGS ('Y', 'N'), it would be best to use:

   new Boolean($F{SFPAR_GRADE_EXEMPT_FLG}.equals("Y"))

But I have not seen anything that definitively (or otherwise) states how to check for a 'not equals' condition. The only things I can guess at are:

   new Boolean(!$F{SFPAR_GRADE_EXEMPT_FLG}.equals("Y"))


   new Boolean($F{SFPAR_GRADE_EXEMPT_FLG} != ("Y"))

What would be the best way to check for a 'not equals' condition? And why? Keeping in mind that I want to use the 'equals' condition check for the checkbox with the check in it, and the 'not equals' condition for the empty checkbox.

Also, why is '.equals' preferable to ' == ', when the report compiles with the '==' condition, and 'appears' to work (when it doesn't).
thosp8246's picture
Joined: Sep 16 2006 - 12:20pm
Last seen: 13 years 10 months ago

1 Answer:

This is a general aspect of Java.
If you have to Strings
y1 = new String("Y");
y2 = new String("Y");

y1==y2 is false, as the object references are compared.
y1.equals(y2) is true, as the objects' content is compared.

Thus, even using constant strings on one side, you always have to use equals (or equalsIgnoreCase) with strings. Otherwise, you get unexpected "is not equal"s.

new Boolean(!($F{SFPAR_GRADE_EXEMPT_FLG}.equals("Y")))
new Boolean(!($F{SFPAR_GRADE_EXEMPT_FLG}.equalsIgnoreCase("Y")))
is correct.

By the way, using N or Y for boolean database fields is not a good custom. Try to use short integers (smallint, int, etc.), with the values "0" for false and "1" (or any other numeric value) for true.
You get much less problems when mapping DB fields to variables, then.

csbac's picture
Joined: Mar 12 2007 - 7:42pm
Last seen: 13 years 5 months ago