# Integer.parseInt("+000000");

Dirk Schreckmann
Sheriff
Posts: 7023
Hello,
Does anybody knows why this code:

int i = Integer.parseInt("+000000");

produces a NumberFormatException, but this code:

int i = Integer.parseInt("-000000");

does not error and produces a '0' just fine?
It's not a show stopper, my work around is:

int i = Integer.parseInt("+000000".substring(1));

I'm just curious about what's going on.
Thank You,
-Dirk Schreckmann
[ February 13, 2002: Message edited by: Dirk Schreckmann ]

Rob Ross
Bartender
Posts: 2205
I will now teach you how to fish my lad
First, you have the source code to all the java classes. It's in a jar file called src.jar, in your jdk folder. Open it up and take a look at the Integer class, then scroll down to the parse() method. You'll see that parse(String) calls parse(String, int).
Looking at this method, we see the following code...

So right away I see their algorithm is only looking for a negative sign as a possible first-character position symbol. It appears that they didn't write any code to handle the plus case.
Finding this and odd omission, I take my fishing poll and sally forth to the Bug Parade at:
Doing a search for Integer.parseInt, I find 346 matches. Scanning them, item #6 catches my eye:
Bug ID: 4296955 Unary plus not recognized by Integer parsing
Looks promising, so I click on it
and here's what we find:

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>
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.

Which describes the problem we are having exactly.
Scrolling down, we find the Sun engineer's comments:

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.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
Rob, that was really well done! Take a bow.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Indeed - good job! I'd also like to recommend one other particularly good fishing spot which shouldn't be overlooked. Happy fishing...

Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065

Dirk Schreckmann
Sheriff
Posts: 7023
And if we really want to be up to date...
Now, how do I thread the line through the hook and tie my knots?
How do I open the src.jar file?
Thank You
[ February 15, 2002: Message edited by: Dirk Schreckmann ]

Dirk Schreckmann
Sheriff
Posts: 7023
How To Open A Jar
But I still can't figure it out.

Dirk Schreckmann
Sheriff
Posts: 7023
OK,
So the Java Archive Tool privided with the SDK will do the job. The basics of its use are described here:
Using JAR files: The Basics
Anybody know of a free graphics based tool available? This command line stuff causes nightmarish flashbacks of the Days Of DOS.

Thank You
[ March 06, 2002: Message edited by: Dirk Schreckmann ]

Manfred Leonhardt
Ranch Hand
Posts: 1492
Hi Dirk,
WinZip free for a while: