2012-06-24 71 views
4

我期望下面的代碼拋出一個ParseException,但它是「2」任何人都可以解釋這個還是這是一個在Java實現中的錯誤?我在最新的Oracle(1.7.0_05)和osx jvms上運行它。NumberFormat.parse返回意想不到的結果

try { 
     NumberFormat format = NumberFormat.getInstance(Locale.US); 
     Number number = format.parse("2 3"); 
     System.out.println(number); 
    } catch (ParseException e) { 
     System.out.println("2 3 is not a valid number!"); 
    } 

回答

2

這是因爲NumberFormat.parse(String)可能不會使用整個文本進行解析。見here

1

這是預期的結果。按照documentation,NumberFormat.parse不一定使用整個字符串。編程規則#1:總是閱讀文檔!