Expression Help!

Hi there.

 

I am trying to do a simple (or so I thought) print when expression where I am comparing the value of a field to a number.

 

The problem is that my field is stored in a string, so I wanted to convert the string to a number somehow and see if it is > 71.

 

Here is what I have so far that doesn't compile, but it should give you the gist of what I am trying to do.

 

Integer.valueOf($F{cycleNumber}) > 71

 

I can't really find any good expression guides out there either. Any help would be greatly appreciated.

Mike

michaelloveusa's picture
Joined: May 5 2011 - 7:14am
Last seen: 12 years 5 months ago

2 Answers:

If you select the very top node in the tree that appears in your Repository Navigator and then look at the "Language" property over in the Properties pane, what language do you have selected?  Groovy?  Java?  JavaScript?  I normally have "Groovy" as a default, and it's pretty forgiving when it comes to data types.  I just wrote a report with the following query:

WITH
  SAMPLEDATA (characterNumber) AS (
     values ('1'), ('3'), ('15'),('25'), ('135')
)
SELECT
  SAMPLEDATA.characterNumber
FROM
  SAMPLEDATA

For my report, I dragged the characterNumber into one column and I created a new text field in another column where I made a label that said "> 20?" and I defined the  Text Field Expression as

$F{characterNumber} > 20 ? "Yes" : "No"

Now, my gut tells me that no language should be so case-lenient that you can compare a string to a literal integer value without attempting some kind of type conversion, but actually, it worked just fine...when I set my language to JavaScript.  It didn't work with Groovy or with Java.

I then tried changing the expression to the following:

Integer.parseInt($F{CHARACTERNUMBER}) > 20 ? "Yes" : "No"

This worked in Java and Groovy, but not in JavaScript.

Hope this helps.



Post Edited by cbarlow3 at 06/24/2011 22:12
cbarlow3's picture
14756
Joined: Mar 4 2010 - 8:59am
Last seen: 1 year 6 months ago

Your suggestions were very helpful.

 

I was using groovy and here is what I ended up getting to work:

 

new Boolean((Integer.parseInt($F{cycleNumber})) > 71)

michaelloveusa's picture
Joined: May 5 2011 - 7:14am
Last seen: 12 years 5 months ago
Feedback
randomness