Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No "tryParseDouble" in Java? [duplicate]

Tags:

java

Possible Duplicate:
How to check a String is a numeric type in java

I see I can parse a String into a double by using Double.parseDouble.

Since that one might throw a NumberFormatException, and I don´t want to do programming by exception, I hesitate to put the parseDouble call into a try/catch block just to check if the string can be parsed into a double.

In Delphi, there is TryParseInt (I think), which returns a defined value (-1 I think) if the string cannot be parsed into an Integer.

There is nothing like that in Java? I mean: there is no standard way to do this?

like image 600
TheBlastOne Avatar asked Mar 08 '12 14:03

TheBlastOne


3 Answers

The standard way to do this would be:

double d;
try {
    d = Double.parseDouble(input);
}
catch (NumberFormatException e) {
    // Use whatever default you like
    d = -1.0;
}

This can of course be wrapped up as a library method if you like.

On the whole I don't miss this not being part of the language - if a string is badly formed, it doesn't represent -1 so the correct thing to do is throw an exception. If you want to treat this as -1 you're free to do so, but there's very weak rationale to make this standard library behaviour (why -1 and not 0 or NaN or null?).

like image 183
Andrzej Doyle Avatar answered Oct 02 '22 08:10

Andrzej Doyle


The problem you have is that you have two possible outcomes. Either you have a valid double or you don't. If you have a return value you need to check for you might forget to check or you have an if check for every value.

try {
    double d = Double.parseDouble(input);
    double d2 = Double.parseDouble(input2);
    double d3 = Double.parseDouble(input3);
    double d4 = Double.parseDouble(input4);
    // all number are good.

} catch (NumberFormatException e) {
    e.printStackTrace(); //prints error
}

or

double d, d2, d3, d4;
if (tryParseDouble(input)) {
    d = parseDouble(input);

    if (tryParseDouble(input2)) {
        d2 = parseDouble(input2);

        if (tryParseDouble(input3)) {
            d3 = parseDouble(input3);

        } else {
            if (tryParseDouble(input4)) {
                d4 = parseDouble(input4);

            } else {
                System.out.println("Cannot parse " + input4);
            }

            System.out.println("Cannot parse " + input3);
        }

    } else {
        System.out.println("Cannot parse " + input2);
    }

} else {
    System.out.println("Cannot parse " + input);
}
like image 21
Peter Lawrey Avatar answered Oct 02 '22 08:10

Peter Lawrey


You can always make some factory class

class DoubleFactory{

public static double tryParseDouble(final String number){

double result;
try {
    result = Double.parseDouble(number);
}
catch (NumberFormatException e) {
    result = 0.0;
}
return result;
}
}

But there is huge problem with that. Your program will continue its normal flow, but some of your model classes will be 'broken'. And after some other operation this 'default' value will pop-up, and broke other , and other. And the worst of all, you will not see the exception leading for these broken results. At least you can

catch (NumberFormatException e) {
    //add exception logging here, something like
    logger.info(e.getMessage());
    result = 0.0;
}

but the result will be the same - operations using default 0.0 (or -1.0 or whatever) value leading to some unrecoverable state.

like image 21
Kiril Kirilov Avatar answered Oct 02 '22 09:10

Kiril Kirilov