JDK-6676634 : Numeric classes need static checker methods for Strings
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-03-18
  • Updated: 2011-02-16
  • Resolved: 2008-04-02
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
The only way to check if a numeric String has a valid format is to try to parse it an catch the exception thrown. I'm requesting an easy way to make sure that a String has a valid numeric format.

JUSTIFICATION :
Writing this kind of code to perform a basic task, to find out the validity of a number, is not acceptable.

boolean isNumeric = true; try{Integer.parseInt(fieldValue);}catch(Exception e){isNumeric = false;}

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
boolean isNumeric = Integer.isInt(s);

boolean isNumeric = Integer.isInt(s,radix);

Similar methods should be implemented in the Byte, Short, Integer, Long, Float and Double classes.

Comments
EVALUATION Yes, it would be preferable if there were a non-exceptional way to convert strings to numerical values, a duplicate of request 4487183. For many years, the javadoc of the Double.valueOf(String) method http://java.sun.com/javase/6/docs/api/java/lang/Double.html#valueOf(java.lang.String) has included code for a regular expression to check the validity of input strings. It is possible to have a regular expression check for legal integer strings too. Integer values range from -2147483648 to 2147483647 All 9 digit strings are acceptable; the problem comes with how to specify the "ragged" 10 digit strings which are acceptable. So, the base regular expression is "(-)?"+ // optional leading minus sign "0*"+ // leading zeros "(\p{Digit}{1,9})" // numbers with 1 to 9 digits For the 10 digit strings, if the leading digit is less than the maximum digit, the other digits can have any value, 1(\p{Digit}{9}) but if the first digit is at it's maximum the next digit must be less than or equal to its maximum 2[0-1]... however, if the second digit is at its maximum the third digit must be less than or equal to its maximum, and so on for all the digits: 1(\p{Digit}{9})| 20(\p{Digit}{8})| 21[0-3](\p{Digit}{7})| 214[0-6](\p{Digit}{6})| 2147[0-3](\p{Digit}{5})| 21474[0-7](\p{Digit}{4})| 214748[0-2](\p{Digit}{3})| 2147483[0-5](\p{Digit}{2})| 21474836[0-3](\p{Digit}{1})| 214748364[0-7] This regular expression can be ORed inside the grouping for numbers with 1 to 9 digits listed above. Finally, a separate case for the most negative value must be added: -2147483648 This kind of ragged pattern will exist for bases that aren't powers of 2 (bases other than 2, 4, 8, 16, and 32). For base 16 the values range from -80000000 to 7fffffff so the regular expression can simply be ((-)?0*(\p{XDigit}{1,7}| [0-7](\p{XDigit}{7}))| (-0*80000000) So, to generalize the regular expression generation to any given base, the algorihtm is 1. Create a canonical version of the string which only uses ASCII characters (the "digits" in the regex may be ASCII letters or digits). 2. For the base in question, generate the strings for MIN_VALUE and MAX_VALUE using the static toString method. 3. Find n, the length of the MAX_VALUE string. Signed strings of that base with (n-1) or fewer characters are all valid inputs. 4. Create a regex for strings of length n: 5. Add a separate regex for the most negative value, -(0*)MinValueString Working out any needed details is left as an exercise for the reader. Closing as duplicate.
02-04-2008