It's not a secret anymore!
The moose likes Beginning Java and the fly likes Integer.parseInt( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer.parseInt("+000000");" Watch "Integer.parseInt("+000000");" New topic


Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Rob Ross

Joined: Jan 07, 2002
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:
You have to sign up to access it, but it's free.
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:
String deltaString = "+93";
int delta = Integer.parseInt(deltaString);
throws the following exception:
java.lang.NumberFormatException: +93
at java.lang.Integer.parseInt(
at java.lang.Integer.parseInt(
The code for Integer.parseInt() contains this fragment:
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw new NumberFormatException(s)
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.

And there's our answer!

SCJP 1.4
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Rob, that was really well done! Take a bow.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Indeed - good job! I'd also like to recommend one other particularly good fishing spot which shouldn't be overlooked. Happy fishing...

"I'm not back." - Bill Harding, Twister
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
Jim meant this link.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
And if we really want to be up to date...
How about this link?
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

Joined: Dec 10, 2001
Posts: 7023
I found this advice:
How To Open A Jar
But I still can't figure it out.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Jan 09, 2001
Posts: 1492
Hi Dirk,
WinZip free for a while:
PkZip free for a while:
I agree. Here's the link:
subject: Integer.parseInt("+000000");
It's not a secret anymore!