No "tryParseDouble" in Java?

Possible duplicate:
How to check string is a numeric type in java

I see that I can Double.parseDouble String to double using Double.parseDouble .

Since this could throw a NumberFormatException , and I do not want to do the programming except, I hesitate to put the parseDouble call in the try / catch to check if the string can be parsed for double .

In Delphi there is a TryParseInt (I think) that returns a specific value (-1, I think) if the string cannot be parsed in Integer .

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

+6
source share
4 answers

The problem is that you have two possible outcomes. Either you have a valid double or not. If you have a return value that you need to check, you can forget to check or you have a check for each 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); } 
+4
source

The standard way to do this is:

 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.

In general, I will not miss this without being part of the language - if the string is poorly formed, it does not represent -1 , so the right thing is to throw an exception. If you want to treat this as -1 , you can do it, but there is very little justification for this behavior of the standard library (why -1 , not 0 or NaN or null ?)).

+6
source

You can always make a 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 a huge problem with this. Your program will continue to flow normally, but some of your model classes will be broken. And after some other operation, this default value will pop up, and the other will be different. And worst of all, you will not see an exception leading to 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 the default value of 0.0 (or -1.0 or another), leading to some unrecoverable state.

+1
source

As an alternative to the solution already provided, you can use regex:

 Pattern doublePattern = Pattern.compile("^\d*$"); Matcher matchesDouble = doublePattern.matcher(myString); boolean isDouble = matchesDouble.matches(); 

or

 boolean isDouble = Pattern.matches("^\d*$", myString); 
0
source

Source: https://habr.com/ru/post/910253/


All Articles