Menu
Topics Index
...
`

Number Format


If you need explanation Read this topic

If you need Answer Take test on this topic

Number Format
What will be the output of the following program?
import java.text.*;

public class Number {
    public static void main(String str[]) {
        try {
            String num = "12one3456.789";
            NumberFormat form = NumberFormat.getInstance();
            System.out.println("Before parse the number is : " + form.parse(num));
            System.out.println("After parse the number is : " + form.parse(num));
        } catch (ParseException pe) {
            System.out.println("Parse Exception thrown");
        }
    }
}

A. Before parse the number is : 12
After parse the number is : 12
B. Before parse the number is : 3456.789
After parse the number is : 3456
C. Before parse the number is : 3456
After parse the number is : 3456.789
D. Some other output
E. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A. when parse method is invoked it parses the given string to produce a number. So 12 is printed before and after.

Posted by Nikhil Sulibhavi    2014-04-18 13:25:16


We can read and parse strings that contain formatted numbers using NumberFormat. But NumberFormat class will only parse a string until it encounters a non-parseable character (Here the first non-parseable character in num is o). Hence, we get 12 on both the occasions. So, the answer is A.

Posted by Shaileshwar Sharma    2014-04-20 17:10:22


Congratulations Shaileshwar Sharma. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-21 04:11:42


© meritcampus 2019

All Rights Reserved.

Open In App