Jump to content
We've recently updated our Privacy Statement, available here ×

Displaying text conditionally based on integer


jimmo

Recommended Posts

Hi All!

I am an iReport and Java newbie. I would like to display text depending on whether an integer is 0 or 1. (The field is defined as a NUMBE, although it is actually used as a boolean). What I would like to do is something like this:

$F{afgemeld}.equals(1)?"Yes":"No"

In the error output I get

Source text : $F{afgemeld}.equals(1)?"Yes":"No"
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :

Then a few lines later:

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'No' with class 'java.lang.String' to class 'java.lang.Integer'
      at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:127)


I understand it so far that iReport cannot cast a string to an interger, so I was trying to cast the original value "afgemeld" to a string. That is where I am getting stuck. I tired this:

$F{afgemeld}.toString().equals("0")?"Yes":"No"


This also gives me:

Cannot cast object 'No' with class 'java.lang.String' to class 'java.lang.Integer'
      at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:127)

Can someone give me a kick in the right direction? I would be very grateful!

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

I am not sure that this would actually be considered a kick in the right direction or not but for one thing your report is using groovy and not java. You need to either use groovy in your expression or switch the report properties to Java for the language. 

If you switch to Java the expression will need to be something like:

$F{afgemeld}.intValue()==1?"Yes":"No"

I do not know any groovy so I can not help you if you go that direction.

You will notice that as you create new reports iReport defaults to language=groovy and you must remember to make that change to use Java.

I hope that this helps you.

 

Link to comment
Share on other sites

$F{afgemeld}==1?"Yes":"No"


returns



Error filling print... Error evaluating expression :


Source text : $F{afgemeld}==1?”Yes”:”No”


net. sf. jasperreports. engine. fill. JPExpressionEvalException: Error evaluating expression :


Source text : ^$F{afgemeld}==1?’Yes’:”No”


...


Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Yes' with class 'java.lang.String' to class 'java.lang.Integer'      at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:127)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...