Conditional parsing from String to Double

0

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})

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

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!

patrick.krings's picture
Joined: Jun 21 2018 - 1:29am
Last seen: 2 years 3 months ago

0 Answers:

No answers yet
Feedback
randomness