wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer.parseInt("+000000");" Watch "Integer.parseInt("+000000");" New topic
Author

Integer.parseInt("+000000");

Dirk Schreckmann
Sheriff

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

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

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:
http://developer.java.sun.com/developer/bugParade/index.jshtml
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:
<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.

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!


Rob
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
Wanderer
Sheriff

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
Sheriff

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

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
Sheriff

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
Sheriff

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

Joined: Jan 09, 2001
Posts: 1492
Hi Dirk,
WinZip free for a while:
http://www.winzip.com/winzip/download.htm
PkZip free for a while:
http://www.pkware.com/downloads/pkzipwind.html
Enjoy,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer.parseInt("+000000");
 
Similar Threads
help - compiled error
System.arraycopy code?
Please help me with this code... I don't know what I'm missing...
testing html and ubb
Getting a Date from a String