Name: yyT116575 Date: 08/01/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
The primitive wrapper classes have parsers for String values: e.g.
"Double.parseDouble(String)" and "Integer.parseInt(String)". If the String
fails to parse, it throws a NumberFormatException. Unfortunately, handling a
thrown exception can be expensive as far as performance.
I would like to see a method added that would allow the String to be read in
and parsed without needing to test it manually to prevent a
NumberFormatException.
A multi-use method to solve this would be "Double.parseDouble(String, double)"
which would use the double parameter as a default. The user could put in
Double.NaN as a default to indicate a failure. "Integer.parseInt(String, int)"
wouldn't have the advantage of a definite non-integer, but if the user knows a
value that will never occur in the input, which is very likely, this would
provide the means to avoid a NumberFormatException, while allowing the option of
using a substitute value.
A simpler (and less useful) method could be "Double.isDouble(String)" or
"Double.canParseDouble(String)" that would return a boolean indicating whether
the parse would be successful.
_______________________________
It has been stated in the "Java Q&A" that exceptions should only be used to
handle error conditions, and never used to control program flow. Therefore, a
method should be added to allow real-time and time-sensitive applications a
better alternative.
(Review ID: 129232)
======================================================================
###@###.### 10/28/04 01:07 GMT
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
The primitive wrapper classes have parsers for String values: e.g.
"Double.parseDouble(String)" and "Integer.parseInt(String)". If the String
fails to parse, it throws a NumberFormatException. Unfortunately, handling a
thrown exception can be expensive as far as performance.
I would like to see a method added that would allow the String to be read in
and parsed without needing to test it manually to prevent a
NumberFormatException.
A multi-use method to solve this would be "Double.parseDouble(String, double)"
which would use the double parameter as a default. The user could put in
Double.NaN as a default to indicate a failure. "Integer.parseInt(String, int)"
wouldn't have the advantage of a definite non-integer, but if the user knows a
value that will never occur in the input, which is very likely, this would
provide the means to avoid a NumberFormatException, while allowing the option of
using a substitute value.
A simpler (and less useful) method could be "Double.isDouble(String)" or
"Double.canParseDouble(String)" that would return a boolean indicating whether
the parse would be successful.
_______________________________
It has been stated in the "Java Q&A" that exceptions should only be used to
handle error conditions, and never used to control program flow. Therefore, a
method should be added to allow real-time and time-sensitive applications a
better alternative.
(Review ID: 129232)
======================================================================
###@###.### 10/28/04 01:07 GMT
- duplicates
-
JDK-6201013 Add a static method boolan Integer.isInteger(String)
-
- Closed
-
-
JDK-6676634 Numeric classes need static checker methods for Strings
-
- Closed
-
- relates to
-
JDK-4705734 {Float, Double}.valueOf use broken test for whitespace characters
-
- Resolved
-
-
JDK-4067842 Float.valueOf() semantic change not for the better
-
- Closed
-