JDK-4296955 : Unary plus not recognized by Integer parsing
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.2,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 1999-12-05
  • Updated: 2000-04-07
  • Resolved: 2000-04-07
Description

Name: krT82822			Date: 12/04/99


12/4/99 eval1127@eng -- still an issue as of kestrel RA (1.3.0 build "I").  (Bug 4242173 seemed to suggest this might have been fixed by now.)

java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

The parseInt() method of the Integer primitive wrapper class does not recognize
a unary plus as a parseable character.  This code:
<p><pre>
        String deltaString = "+93";
        int delta = Integer.parseInt(deltaString);
</pre><p>
throws the following exception:
<p></pre>
        java.lang.NumberFormatException: +93
	      at java.lang.Integer.parseInt(Integer.java:409)
	      at java.lang.Integer.parseInt(Integer.java:458)
</pre><p>
The code for Integer.parseInt() contains this fragment:
<p><pre>
		digit = Character.digit(s.charAt(i++),radix);
		if (digit < 0) {
		    throw new NumberFormatException(s)
</pre><p>
which will cause the above exception.  The code looks for the minus sign and
bumps the parsing index up; it should also include a test for a plus sign.
(Review ID: 98621) 
======================================================================

Name: rlT66838			Date: 03/17/2000


all versions


Integer.parseInt does not handle strings like "+123".
While this appears to be deliberate (and therefore not a bug)
I think it is silly.  Why not allow it to handle explicit positives?

Integer.parseInt("123") ;   // works
Integer.parseInt("-123");   // works
Integer.parseInt("+123");   // throws NumberFormatException
(Review ID: 102607)
======================================================================

Comments
WORK AROUND Name: krT82822 Date: 12/04/99 Code can be altered to work around this problem like this: <p><pre> String deltaString = "+93"; if (deltaString.startsWith('+')) deltaString = deltaString.substring(1); int delta = Integer.parseInt(deltaString); </pre> ====================================================================== Name: rlT66838 Date: 03/17/2000 Check for strings that start with "+" and remove the "+". (Review ID: 102607) ======================================================================
11-06-2004

EVALUATION While it seems somewhat Draconian not to allow a leading plus sign, the specification is clear on this issue, and the implementation obeys the specification. There is code inside the Java platform libraries that would break if we were to change Integer.parseInt's behavior to allow for a leading plus sign (e.g., BigInteger's String constructor), and it is quite likely that we are not unique in this regard. I strongly suspect that our customers also have code that would break if we were to make the suggested incompatible specification change at this late date. joshua.bloch@Eng 2000-04-07
07-04-2000