The program given below will test your knowledge about float data types in Java.
The program given below will test your knowledge about float data types in Java.Given below the sample code :
1 public class SuperClass {
2
public static void main(String[] args) {
3
System.out.println(stringConvert("0.3"));
4
System.out.println(stringConvert("0.3A"));
5
System.out.println(stringConvert(null));}
6
public static boolean stringConvert(String s) {
7
float factor = 0;
8
try {
9
factor = Float.valueOf(s).floatValue();
10
return true;
11
} catch (NumberFormatException e) {
12
System.out.println("Exception in number formatting " + s);
13
factor = Float.NaN;
14
} finally {
15
System.out.println("Finally");
16
}
17
return false;}
18
}
How can we correct the above code ?
1. By removing line 5
2. By removing line 4
3. No need for correction
4. by removing line 3
(1)
The error due to "0.3A" is caught by the "catch" but the "null" at line 5 cause error in string conversion.