String to double conversion

Report a typo

Consider a method that takes a string and converts it to a double. If the input string happens to be null or of an unsuitable format, a runtime exception occurs and the program fails.

Fix the method so it would catch any exception and return the default value 0 (zero) if an exception occurred.

Sample Input 1:

123.0

Sample Output 1:

123.0

Sample Input 2:

15.5

Sample Output 2:

15.5
Write a program in Java 17
class Converter {

/**
* It returns a double value or 0 if an exception occurred
*/
public static double convertStringToDouble(String input) {
return Double.parseDouble(input);
}
}
___

Create a free account to access the full topic