-2

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: ""

António Paulo
  • 351
  • 3
  • 18
  • 7
    If you wanted to figure it out yourself, why ask it here? – Eran Jul 03 '15 at 16:27
  • 2
    What stops you from finding out? – PM 77-1 Jul 03 '15 at 16:27
  • possible duplicate of [What is difference between Double.parseDouble(string) and Double.valueOf(string)?](http://stackoverflow.com/questions/10577610/what-is-difference-between-double-parsedoublestring-and-double-valueofstring) – ganeshvjy Jul 03 '15 at 16:29
  • 3
    There's no problem to solve here. Closing to vote for being and off-topic question. – Luiggi Mendoza Jul 03 '15 at 16:29
  • 4
    @GaneshThiagarajan I don't think it's a duplicate. That post at least has a valid question, while this one doesn't. – Luiggi Mendoza Jul 03 '15 at 16:29
  • What I wanted to say is: My project is more complex than this. I just need to know the "return" to try something, instead of "hey, solve me this for me". – António Paulo Jul 03 '15 at 16:30
  • I edited now. I just wrote the part that everyone didnt understood right because it didn't let me ask without a amount of characters – António Paulo Jul 03 '15 at 16:33
  • Your giving the program a blank space and you expect it to return a double? – UnknownOctopus Jul 03 '15 at 16:34
  • just tel us what exactly you are expecting as a return – SSH Jul 03 '15 at 16:35
  • I think OP just didn't clearly typed what he is looking for, so it doesn't mean that it should be discarded so brutally. Just my opinion. – hagrawal7777 Jul 03 '15 at 16:38
  • @hagrawal if a question is very unclear, closing is the right thing to do. It's not immediately discarded. The question can still be edited and reopened. In this case, I think it's still very unclear, because it seems to say that he has already seen what happens. If he's seen what happens, his real question must not be "what happens?", because he's seen that... – Dan Getz Jul 04 '15 at 01:28

3 Answers3

1

It'll throw an exception java.lang.NumberFormatException.

Muhammad Imran
  • 734
  • 7
  • 21
0

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
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • Did you get your answer? If not then please write your own answer so that others can be benefited from it .. http://stackoverflow.com/help/accepted-answer – hagrawal7777 Jul 04 '15 at 22:53
0

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.

UnknownOctopus
  • 2,057
  • 1
  • 15
  • 26
  • Thanks. My program return "" when I read a file. That file is inside the device internal storage. Does it return "" because of the double values that I am saving inside it? – António Paulo Jul 06 '15 at 07:41