Jump to content
Changes to the Jaspersoft community edition download ×

Conditional parsing from String to Double


patrick.krings

Recommended Posts

Hey everyone!

I am having a bit of trouble with getting my report with subreports to run. As a datasoure I get some XML and I want to sum up some of the values for specific entries. Basically this works fine, but in addition I have to deal with some Strings that cannot be parsed to Double value. For example I might get "a1.0a" instead of just "1.0" from XML source. It is fine if I just get a 0.0 if the number cannot be parsed.

What I have tried so far is to use the static java.util.regex.Pattern.matches-function like this:

java.util.regex.Pattern.matches(("[\x00-\x20]*" +        "[+-]?(" +        "NaN|" +        "Infinity|" +        "(((" + "(\p{Digit}+)" + "(\.)?(" + "(\p{Digit}+)" + "?)(" + ("[eE][+-]?" + "(\p{Digit}+)") + ")?)|"        +        "(\.(" + "(\p{Digit}+)" + ")(" + ("[eE][+-]?" + "(\p{Digit}+)") + ")?)|" +        "((" +        "(0[xX]" + "(\p{XDigit}+)" + "(\.)?)|" +        "(0[xX]" + "(\p{XDigit}+)" + "?(\.)" + "(\p{XDigit}+)" + ")" +        ")[pP][+-]?" + "(\p{Digit}+)" + "))" +        "[fFdD]?))" +        "[\x00-\x20]*"), $F{MENGE})[/code]

This code basically checks if the field MENGE only contains digits or comma or dots. It returns a boolean, so it can be used in the conditional statement () ? true : false to either do the conversion or just return 0.0. Unfortunately I am just receiving the following exception: Error filling print... Unparseable number: "a1.0a"

The complete code for the Variable Expression looks like this:

java.util.regex.Pattern.matches(("[\x00-\x20]*" +        "[+-]?(" +        "NaN|" +        "Infinity|" +        "(((" + "(\p{Digit}+)" + "(\.)?(" + "(\p{Digit}+)" + "?)(" + ("[eE][+-]?" + "(\p{Digit}+)") + ")?)|"        +        "(\.(" + "(\p{Digit}+)" + ")(" + ("[eE][+-]?" + "(\p{Digit}+)") + ")?)|" +        "((" +        "(0[xX]" + "(\p{XDigit}+)" + "(\.)?)|" +        "(0[xX]" + "(\p{XDigit}+)" + "?(\.)" + "(\p{XDigit}+)" + ")" +        ")[pP][+-]?" + "(\p{Digit}+)" + "))" +        "[fFdD]?))" +        "[\x00-\x20]*"), $F{MENGE}) ? ($F{EINHEIT}.equals("l") ? Double.valueOf($F{MENGE}): ($F{EINHEIT}.equals("m³") ? Double.valueOf($F{MENGE})*1000: ($F{EINHEIT}.equals("ml") ? Double.valueOf($F{MENGE})*0.001: ($F{EINHEIT}.equals("g") ? Double.valueOf($F{MENGE}): ($F{EINHEIT}.equals("t") ? Double.valueOf($F{MENGE})*1000000: ($F{EINHEIT}.equals("kg") ? Double.valueOf($F{MENGE})*1000: ($F{EINHEIT}.equals("mg") ? Double.valueOf($F{MENGE})*0.001: ($F{EINHEIT}.equals("Stk") ? Double.valueOf($F{MENGE}) : Double.valueOf($F{MENGE}))))))))) : 0.0[/code]

If the field MENGE is a number some unit conversion is done and returned. If it is not a number just 0.0 shall be returned. It works perfectly fine for only digits/numbers inside the field MENGE.

 

Might it be true that the conversion of the field MENGE happens in any case and the return value is only used IF the condition returns true?

 

Any help will be appreciated!

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

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