Reputation: 555
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.
Upvotes: -2
Views: 153
Reputation: 44952
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);
}
Upvotes: 1