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

Calculating average of field


teamdebo

Recommended Posts

 Hi, 

I am new to jasperserver and iReports, however I do know java pretty well.

I have a question which has been bugging me for a long time because I can't seem to figure it out and its so simple I should be able to find the answer.

 

I have a field:  fieldA it is a string

I want to calculate the average, min, max and median, but for now I just need to figure out one of those and the rest will be simple.

What I did was create a variable avgFieldA 

class: java.lang.Integer

initial value: java.lang.Integer.valueOf(0)

expression: java.lang.Integer.parseInt($F{fieldA })

 

then I display this variable with: $V{avgFieldA} but I keep getting the following error:

 

 

 

Code:
Error filling print... Error evaluating expression :      Source text : java.lang.Integer.parseInt($F{fieldA}}) Setting up the file resolver...  net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : java.lang.Integer.parseInt($F{fieldB})      at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:259)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluateEstimated(JRCalculator.java:580)      at net.sf.jasperreports.engine.fill.JRCalculator.estimateVariables(JRCalculator.java:179)      at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:787)      at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1482)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:130)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)      at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:878)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: java.lang.NumberFormatException: For input string: "X"      at java.lang.NumberFormatException.forInputString(Unknown Source)      at java.lang.Integer.parseInt(Unknown Source)      at java.lang.Integer.valueOf(Unknown Source)      at sun.reflect.GeneratedMethodAccessor817.invoke(Unknown Source)      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)      at java.lang.reflect.Method.invoke(Unknown Source)      at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)      at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43)      at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88)      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)      at DeviceReport_1307053781917_733717.evaluateEstimated(calculator_DeviceReport_1307053781917_733717:489)      at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:246) 
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

 Ok so I narrowed the proble down a bit but I still don't know how to fix it.

 

It appears as though the parsing actually works, except some of the string data is not numeric so a NumberFormatException is thrown which is normal for java. 

Now the problem is how to get around that (If it is possible).

 

Is there some way to catch the exception, or is there a way to parse the string first, check if it has only numerics and only in that case attempt to parse it into integer?

Does anyone know how to do this?

 

Thanks

Link to comment
Share on other sites

 after even further trial and error hours wasted I came up with this:

 

( $F{fieldA}.matches("[0-9]+") ? new java.lang.Integer($F{fieldA}) : new java.lang.Integer('0'))

 

This appears to work in the sense that it does not throw any exceptions and actually compiles.

Can anyone tell me if this is a good approach or if there is a better approach or maybe this is a complete nubie solution which I would like to not actually implement.

 

Thank you

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