I have the following code to parse a String variable called str.
NumberFormat formatter = NumberFormat.getInstance();
Number number = formatter.parse(str);
I want to catch the Exception thrown when str is not a number just to validate it. The problem I have is that it does't always throws the ParseException expected. When the String str starts with a number but then are characters it seems to get a the first characters of the String and parse them as a number.
For example:
I cannot use Double.parseDouble(str) because str can have commas and points like 1,000.98 and this format is not understood by this method.
Why is this happening? Can I validate it in any other way? Thanks
The behaviour is not strange, it's as designed
Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
You may use the position-aware parsing method like this:
public static double parse(String str) throws ParseException {
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition position = new ParsePosition(0);
Number number = formatter.parse(str, position);
if (position.getIndex() != str.length()) {
throw new ParseException("failed to parse entire string: " + str, position.getIndex());
}
return number.doubleValue();
}
If you look at the API, it clearly says:
Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
If you want to see how far the parser parsed, you can use the other position-aware method. This way you can check if you have any trailing chars. You could also check the whole string for alphanumeric chars using for instance common langs isAlpha.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With