I want to know that will happen if I convert this:
String ret = "";
to double:
Double.parseDouble(ret);
When I run the problem I have a error saying Invalid double: ""
I want to know that will happen if I convert this:
String ret = "";
to double:
Double.parseDouble(ret);
When I run the problem I have a error saying Invalid double: ""
See below Java's implementation of Double.parseDouble
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.readJavaFormatString(s).doubleValue();
}
Now check below code FloatingDecimal.readJavaFormatString here
in = in.trim(); // don't fool around with white space. throws NullPointerException if null
int l = in.length();
if ( l == 0 ) throw new NumberFormatException("empty String");
To answer your question: Since you are passing empty string, you will get NumberFormatException.
Exception you will get as below. Notice the message ("empty String") is same as what is can be seen in my second code snippet.
Exception in thread "main" java.lang.NumberFormatException: empty String
If you try to run the code
String ret = "";
double a = Double.parseDouble();
The compiler will throw a java.lang.NumberFormatException, meaning, in plain terms, you gave the program a type of input that could not be converted to a double. If you want to fix this then just give the program a parse able string (i.e 6, or 3.24). You could also use try and catch to throw different error messages if the wrong input is given.
Example:
public class Testing {
public static void main(String[] args) {
try{
String ret = "";
double a = Double.parseDouble(ret);
}catch(NumberFormatException e){
System.out.println("Your error message here");
//do something (your code here if the error is thrown)
}
}
}
This would print out Your error message here, because the input "" can't be converted to a double.
More on NumberFormatException: Click here.
More on parsing strings to doubles: Click here.
More on try and catch: Click here.