开发者

Strange behaviour of NumberFormat Java

开发者 https://www.devze.com 2022-12-20 09:24 出处:网络
I havethe following code to parse a String variable called str. NumberFormat formatter = NumberFormat.getInstance();

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开发者_JS百科 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:

  • if str="a10" then is thrown a ParseException
  • if str="10a" then no exception thrown and number=10

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消