Matthew Sowders
Matthew Sowders

Reputation: 1680

NumberFormat.parse is returning unexpected result

I would expect the following code to throw a ParseException, but it is "2" can anyone explain this or is this a bug in the java implementation? I am running it on the most recent Oracle (1.7.0_05) and osx jvms.

    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!");
    }

Upvotes: 4

Views: 250

Answers (2)

jahroy
jahroy

Reputation: 22692

That is the expected result.

According to the documentation, NumberFormat.parse does not necessarily use the entire string.

Rule #1 of java programming: always read the documentation!

Upvotes: 1

javaCity
javaCity

Reputation: 4318

It is because NumberFormat.parse(String) may not use the entire text for parsing. See here.

Upvotes: 2

Related Questions