Jump to content

Count Function


Case1182

Recommended Posts

I have not been able to find a working count function in iReports, have tried different formats of java, sql, and grping in iReports. Want to count all the strings match x in y column. Any suggestions let me know.
Link to comment
Share on other sites

  • Replies 17
  • Created
  • Last Reply

Top Posters In This Topic

You have to imagine your expressions as being an inline piece of Java (because that's what they will be when the xml is interpreted by the report engine!).

 

All of the expression boxes will resolve to a specific java class (eg. java.lang.String or java.lang.Integer or java.util.Date) as specified by you.

 

Your raw expressions will always result in a primitive such as string, integer, or boolean. The construct for a primitive does not match the construct for a class, so you'll always end up with an exception.

 

So you need to recast as part of your expression.

 

Let's say you have an expression box that expects a java.lang.Boolean to be returned. If you enter an expression like this:

 

$V{REPORT_COUNT}.intValue>1

 

then the result will be boolean and you'll get an exception because boolean != Boolean. You need to wrap it in a cast to Integer like this:

 

new java.lang.Boolean($V{REPORT_COUNT}.intValue>1)

Link to comment
Share on other sites

I am trying to do something like this:

 

If ($F{NAME}.toString() == $P{NAME}.toString())

{$F{COUNT}.toString() = $F{COUNT}.toString() + 1}

 

It will not except the full java statement, does not recongize things like ELSE, it says this is fine but when I go to execute it gives this error.

 

it.businesslogic.ireport.gui.logpane.ProblemItem@186b73e Syntax error, insert ")" to complete Expression Line 262, Column 53 /jasperReport/title[1]/band[1]/textField[4]/textFieldExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@1687b84 Syntax error, insert ";" to complete BlockStatements Line 262, Column 53 /jasperReport/title[1]/band[1]/textField[4]/textFieldExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@855040 Syntax error on tokens, delete these tokens Line 262, Column 53 /jasperReport/title[1]/band[1]/textField[4]/textFieldExpression[1]

it.businesslogic.ireport.gui.logpane.ProblemItem@57b4b0 Syntax error, insert "}" to complete Block

it.businesslogic.ireport.gui.logpane.ProblemItem@179d59 Syntax error, insert "}" to complete Block

it.businesslogic.ireport.gui.logpane.ProblemItem@5b5ff5 Syntax error, insert "}" to complete Block

Link to comment
Share on other sites

Lots of issues here:

 

 

First, you can't use If..Then..Else..Endif. You have to use ( comparison ? action when true : action when false )

 

 

Next up $F{NAME} and $P{NAME} are likely to be Strings already so there's no need to perform a .toString on them.

 

 

It is very bad karma to perform a String==String comparison. Use .equals() instead.

 

 

You can't change the value of a field, so trying to increment $F{COUNT} won't work. Use a variable instead.

 

 

Using reserved words like NAME and COUNT as variable, field or parameter names is asking for trouble.

 

 

--------------------

 

 

The solution is to set up a java.lang.Integer variable called $V{myCOUNT} with an Initial Value Expression of new java.lang.Integer(0) and a Calculation Type of "Sum".

 

 

Then set your Variable Expression to new java.lang.Integer($F{NAME}.equals($P{NAME} ? 1 : 0 ). This will increment $V{myCOUNT} whenever the contents of $F{NAME} match the contents of $P{NAME} otherwise do nothing.

 

.

Post edited by: jmurray, at: 2007/05/09 23:13

Link to comment
Share on other sites

Got it to work like this except it doesnt increment the value.

 

new java.lang.Integer($F{NAME}.toString() + "%" == $P{NAME}.toString() ? $V{myCOUNT}.intValue() + 1 : $V{myCOUNT}.intValue() + 0 )

 

the "%" is because it is a oracle database and requires it at the end of the parameter. any ideas?????

Link to comment
Share on other sites

Yes, there was a typo in my posting! Bugger.

 

 

The Variable Expression was missing a closing parenthesis, so enter this instead: new java.lang.Integer($F{NAME}.equals( $P{NAME} ) ? 1 : 0 )

 

 

Everything else stays the same. You must stop using "==" to compare Strings. Very, very much badness.

Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...
  • 1 year later...
  • 3 years later...
  • 3 months later...
  • 3 months later...

The below mentioned solution worked for me but keep in mind that uou have to set the resest type of variable to " Report"

The solution is to set up a java.lang.Integer variable called $V{myCOUNT} with an Initial Value Expression of new java.lang.Integer(0) and a Calculation Type of "Sum".


Then set your Variable Expression to new java.lang.Integer($F{NAME}.equals($P{NAME} ? 1 : 0 ). This will increment $V{myCOUNT} whenever the contents of $F{NAME} match the contents of $P{NAME} otherwise do nothing.

 

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