简体   繁体   中英

NumberFormatException on StringUtils.isBlank method

I am getting a NumberFormatException although I have StringUtils.isBlank() and I also added a check for a non-breakable white space character, as mentioned in below code:

if (isBlank(amtBeforeTax) || amtBeforeTax.matches("^[\\p{Z}]*$")) {
                ra.setAmtBeforeTax(BigDecimal.ZERO);
            } else {
                ra.setAmtBeforeTax(new BigDecimal(amtBeforeTax));
            }

Still I am getting a number format exception on the on the above piece of code. I do not have the control over the amtBeforeTax , It's a stream of data I am getting and just setting it to some other object. I wanted to know what exactly the precussion i will take over here to avoid the exception.

One way to solve it is to catch the NumberFormatException , effectively using BigDecimal constructor to perform the validation instead of writing the rules yourself:

try {
  ra.setAmtBeforeTax(new BigDecimal(amtBeforeTax))
} catch (NumberFormatException ex) {
  ra.setAmtBeforeTax(BigDecimal.ZERO);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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