Menu
Topics Index
...
`

Creating NumberFormat


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating NumberFormat
Compare the following program?
import java.text.*;

public class NumPar1 {
    public static void main(String str[]) {
        try {
            String number = "23456.789one";
            NumberFormat format = NumberFormat.getInstance();
            System.out.println("Before parse the number is : " + format.parse(number));
            format.setParseIntegerOnly(true);
            System.out.println("After parse the number is : " + format.parse(number));
        } catch (ParseException pe) {
            System.out.println("Parse Exception thrown");
        }
    }
}

import java.text.*;
public class NumPar2 {
    public static void main(String str[]) {
        try {
            String number = "one23456.789";
            NumberFormat format = NumberFormat.getInstance();
            System.out.println("Before parse the number is : " + format.parse(number));
            format.setParseIntegerOnly(true);
            System.out.println("After parse the number is : " + format.parse(number));
        } catch (ParseException pe) {
            System.out.println("Parse Exception thrown");
        }
    }
}

A. Both NumPar1 and NumPar2 produce same output.
B. NumPar1 throws an exception, but NumPar2 will compile.
C. NumPar2 throws an exception, but NumPar1 will compile.
D. Both NumPar1 and NumPar2 compile, but they produce different output.
E. Both NumPar1 and NumPar2 throws an exception.
Topic: Unknown

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.

C is the answer ! Numpar2 throws an excpetion since setParseIntegerOnly firstly looks for the integer but here ONE is found that is character ! thus throws an exception ! where as first program complies successfully !

Posted by Shaik Nyamath    2013-08-28 12:43:46


answer is c because parse method throws parse exception if beginning of string is not parsable

Posted by Shashanka Mogaliraju    2013-08-28 12:43:52


C is the correct because in the first case we are parsing only Integer part only which doent contain characters so it compiles fine in the second case integer value is concatinater with characters so it cannot be parsed to Integer so it throws an exception

Posted by Ram Reddy Bitla    2013-08-28 12:46:20


answer is c ,only numpar2 will throw an exception because the starting of the string is not a number.

Posted by Shiva Goud    2013-08-28 12:57:43


Congratulations Shaik Nyamath. You are this dose winner. Please send us your mobile details in a private message and we will process the recharge asap.

Posted by Merit Campus    2013-08-29 03:27:04


Got RC :)

Posted by Shaik Nyamath    2013-08-29 11:12:40


© meritcampus 2019

All Rights Reserved.

Open In App