简体   繁体   English

如何在jasperReport中对字符串列求和

[英]How to claculate string column Sum in jasperReport

I need to retrieve sum of a String column so i wrote this : 我需要检索一个String列的总和,所以我这样写:

<variable name="totalQt" class="java.lang.Integer" calculation="Sum">
    <variableExpression>
      <![CDATA[Integer.parseInt($F{QTARTBP}.replaceAll(" ", ""))]]>

     </variableExpression>
</variable>

But i'm getting this error : 但我收到此错误:

....
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at   
 articleBonPreparation_1385730226859_237481.evaluateEstimated(articleBonPreparation_1385730226859_237481:428)

 at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:254)

So there are empty string , so how to use zero in that case , i wrote thiw but it's wrong : 所以有一个空字符串,所以在这种情况下如何使用零,我写了thiw,但这是错误的:

    <variableExpression>

      <![CDATA[$F{QTARTBP}.equals("") ? 0 : Integer.parseInt($F{QTARTBP}.replaceAll(" ", ""))]]>

    </variableExpression>

Any idea will be appreciated 任何想法将不胜感激

It would be useful to see what input data you are using, but despite that, it's clear that your code will fail if the value of $F{QTARTBP} is a single space ( " " ). 查看正在使用的输入数据将很有用,但是尽管如此,如果$F{QTARTBP}的值是单个空格( " " ),很显然代码将失败。 You could try the following expression instead, which will include the single space in your empty string check and will return 0 instead: 您可以尝试使用以下表达式,该表达式将在空字符串检查中包含单个空格,而将返回0

$F{QTARTBP}.replaceAll(" ", "").equals("") ? 0 : Integer.parseInt($F{QTARTBP}.replaceAll(" ", ""))

You should be aware that this is will still fail if any of your input strings cannot be interpreted as an integer. 您应该意识到,如果您的任何输入字符串都不能解释为整数,这仍然会失败。

Jasper are not to calculate - you should generate String in your java-code. Jasper不会计算-您应该在Java代码中生成String。 And calculate what you need in java too. 并计算您在Java中所需的资源。

Upd. 更新。 This answer is incorrect. 这个答案是不正确的。 I learned JR more deeper and understand it. 我更深入地了解了JR,并了解了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM