If Then Else Statement

Good Afternoon,


I hav ebeen fighing with this for a while just trying to learn Jasper as it is not like may other languages I have seen. Im trying to do a simple If then statement but for some reason everything I read it is not working the way I need it to.

Using the smaple DB that comes with Jaspersoft Im trying to make the SHIPCOUNTRY Column any time it is USA I want it to read States and if not USA I would like it to not say anything.

When searching on the community and many other places I came up with a couple different options and none seem to be working Please help The below code is what I have found.

$F{SHIPCOUNTRY} == "USA" ? "States" : "" Am I even in the ball park? Im using 6.4.2 version. Thank you for any help.

jmayhallfb23's picture
Joined: Feb 23 2018 - 12:38pm
Last seen: 5 years 7 months ago

Ahhh Ha.

After doing some good Searching I found it. StackOverflow know alot about Jasper Also.

$F{SHIPCOUNTRY} .equals("USA") ? "States" : ""

Im still wondering what language this may be. Is it Jaspersoft Language.

jmayhallfb23 - 5 years 7 months ago

Now another question arises. What if one wants to Do the same condition but wants it to say "Good Vacation" any time "USA" or "France" or "Germany" appears in the condiditon?

jmayhallfb23 - 5 years 7 months ago

2 Answers:

You can also utilize the Built-in Function provided by Jasper Report lib. Jasper recently added the IF(condition, true , false ), should be 6.4 up. 
So the above example you gave will work or you can use the build in IF function. IF($F{SHIPCOUNTRY}.equals("USA"),"Good Vacation","Negative")
I would recommend using the .equals() or .contains() function to compare strings and not the == operator

Also you could set jasper report compile to groovy if you prefer groovy, all java code should compile as groovy uses java, it just a shorted hand version of coding, (less complicated and compiler is more self-intelligent to a degree)

So based on this comment you added: 
Now another question arises. What if one wants to Do the same condition but wants it to say "Good Vacation" any time "USA" or "France" or "Germany" appears in the condition?
Nested If example : 
$P{SHIPCOUNTRY}.equals("USA") ? "Good Vacation" 
: $P{SHIPCOUNTRY}.equals("France") ? "Good Vacation" : 
$P{SHIPCOUNTRY}.equals("Germany") ? "Good Vacation" : "Negative"

Example that more applicable to your question: 
$P{SHIPCOUNTRY}.equals("USA") || $P{SHIPCOUNTRY}.equals("France") || $P{SHIPCOUNTRY}.equals("Germany") ? "Good Vacation" 
:  "Negative"

joseng62's picture
Joined: Dec 5 2014 - 2:43am
Last seen: 8 months 1 day ago

By default, JasperReports is using Java. "condition ? true case : false case" is just Java ternary operator. Recommend that you first Java. If you are going to be using a database, also recommending learning sql.



JasperReports also supports Groovy but I think most of us use the default Java.

hozawa's picture
Joined: Apr 24 2010 - 4:31pm
Last seen: 4 years 3 months ago