Menu
Question Index
...

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


Both NumPar1 and NumPar2 produce same output.
NumPar1 throws an exception, but NumPar2 will compile.
NumPar2 throws an exception, but NumPar1 will compile.
Both NumPar1 and NumPar2 compile, but they produce different output.
Both NumPar1 and NumPar2 throws an exception.

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App