1

I have to add 'id' (String) for barcode in iReport.

The id is of 8 digits (this could vary), I want to add 4 leading 0's to it. So that the length of id becomes 12.

jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PadStringNumber" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="07c0dc81-9b58-48f9-8129-e0e08fe1cb98">
    <parameter name="id" class="java.lang.String">
        <defaultValueExpression><![CDATA["23423552"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="39" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="550" height="30" uuid="26317c0f-77c0-46cb-a4d8-1ee4fa1e7387"/>
                <textFieldExpression><![CDATA[String.format("%12d",$P{id})]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

My current error is:

Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: String.format("%12d",$P{id})
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:291)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:618)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:586)
    at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1020)
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:567)
    at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:551)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:281)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:504)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:315)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:251)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:119)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:558)
    at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFill.run(BaseFillHandle.java:119)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at PadStringNumber_1566475648252_841187.evaluate(PadStringNumber_1566475648252_841187:178)
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:277)
    ... 13 more
Alex K
  • 22,315
  • 19
  • 108
  • 236
Aebi
  • 19
  • 4
  • @PetterFriberg The ID is not fixed, length could vary, but I need a fix length of 12 to be displayed for the barcode. I have tried String.format("%12d", $F{myBarcode}), it throws this error : " Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException" – Aebi Aug 22 '19 at 11:31
  • Ok, show a minimal version of the jrxml [mcve] and the exact error you get (stacktrace) [edit] the question. – Petter Friberg Aug 22 '19 at 11:37
  • In my DB the id value is integer, I am converting to String in java class and further using the data. It shows this as another cause, "Caused by: java.util.IllegalFormatConversionException: d != java.lang.String" – Aebi Aug 22 '19 at 11:45
  • Yeah you need to parse it to Integer (or pass it as Integer) or use a lib, if you edit, probably I can answer – Petter Friberg Aug 22 '19 at 11:46
  • Just make a small new jrxml with this problem and show stacktrace – Petter Friberg Aug 22 '19 at 11:49
  • For making another jrxml, I'll have to import multiple jars for fetching data. Can you tell me, how can I parse the ID in iReport? – Aebi Aug 22 '19 at 11:57
  • Hi, it's done. I did this- String.format("%0" +12+ "d", Integer.parseInt($P{condition}) – Aebi Aug 22 '19 at 12:07
  • Yeah, this looks similar. Sure, we can reopen and answer. Also, if it is in MoneyBigDecimal format, what should I use to parse? – Aebi Aug 22 '19 at 12:34

1 Answers1

0

The error is related to that you can not format a String with a number pattern %12d, to use this you need parse your String to a number first which naturally can not be done if it is not a number

As a general solution in jasper reports I would use org.apache.commons.lang3.StringUtils.leftPad, jasper reports already have this library as a dependence through commons-collections (no need to install). This method will work with any string even "HelloWorld"

org.apache.commons.lang.StringUtils.leftPad($P{id}, 12, "0")

If you are sure id is always a number you could also parse the string first (it will throw error if it is not a number). I'm using Long since Integer has a max value of 2147483647.

String.format("%012d", Long.parseLong($P{id}))

Full jrxml example

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PadStringNumber" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="07c0dc81-9b58-48f9-8129-e0e08fe1cb98">
    <parameter name="id" class="java.lang.String">
        <defaultValueExpression><![CDATA["23423552"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="177" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="10" width="550" height="30" uuid="b4a554e1-25c2-4bf9-a6f2-5d9b1a937384"/>
                <textElement verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[org.apache.commons.lang.StringUtils.leftPad]]></text>
            </staticText>
            <textField>
                <reportElement x="0" y="40" width="550" height="30" uuid="26317c0f-77c0-46cb-a4d8-1ee4fa1e7387"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[org.apache.commons.lang.StringUtils.leftPad($P{id}, 12, "0")]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="100" width="550" height="30" uuid="64bbd86b-bcf4-41ea-892b-0511fcc9167b"/>
                <textElement verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Long.parseLong()]]></text>
            </staticText>
            <textField>
                <reportElement x="2" y="130" width="550" height="30" uuid="384e670e-b682-4491-9937-64441bd73203"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[String.format("%012d", Long.parseLong($P{id}))]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Result

output

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109