programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Why NumberFormatException ???

Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
hi all,

Does the parsrInt(String str, int radix) method take positive string argument only ......i mean
System.out.println(Integer.parseInt("11111111111111111111111111111101", 2));
throws a NumberFormatException, but 11111111111111111111111111111101 is -3
so it should be valid.....

thank
amit

Ranch Hand
Posts: 121
• Number of slices to send:
Optional 'thank-you' note:
The parseInt method allows negative values, but they must be represented using the "-" sign as prefix, and not in two's complement as you suggest. Thus, the correct way of printing a -3 is:

System.out.println(Integer.parseInt("-11", 2));

Considering what I just explained, if the resulting number doesn't fit into a 32 bit signed int, you get a NumberFormatException.

...Ariel

Amit Das
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
but 11111111111111111111111111111101 is a correct way of representing 32-bit signed int, where the MSB represents sign ......isn't it???

Ranch Hand
Posts: 195
• Number of slices to send:
Optional 'thank-you' note:
Don't confuse two's complement binary notation which purely mathematical base two binary notation. In two's complement, we have something called a sign bit, which is meaningless in pure mathematics. That base-two digit all the way on the left of your number is not a sign bit in base two; it's only a sign bit in two's complement. When you call parseInt and tell it to use a radix of 2, you're saying that the String should be evaluated as a base two number, NOT as a two's complement bit pattern.

So if the leftmost digit is not a sign bit in base two, what is it? It's just another digit. It's the digit that represents the 2147483648 column. So if a 1 is in that column, and there is no negative sign on this ordinary base-two number, the number must be 2147483648 or more, which is a value that will not fit into an int. I suspect that the NumberFormatException is caused by the fact that the String you entered, when translated to base two (not two's complement) is not a valid int value. It's too high.

Here's a short program to see the powers of 2 from 0 to 31.

You entered the number
11111111111111111111111111111101
The rightmost column is the 1 column. The next column is the 2 column, then the 4 column, and so on all the way to the left side, where we reach the 2147483648 column. There's a 1 in that column, and the number is positive because there is no negative sign (remember, there's no "sign bit" in a regular base two number). Therefore, the number is greater than 2147483648, so it's too big for an int.
[ May 11, 2005: Message edited by: Joe Sanowitz ]

Ariel Ortiz
Ranch Hand
Posts: 121
• Number of slices to send:
Optional 'thank-you' note:

but 11111111111111111111111111111101 is a correct way of representing 32-bit signed int, where the MSB represents sign ......isn't it???

Yes it is, if you're using two's complement to represent negative numbers. But just note that the parseInt method doesn't use two's complement.

...Ariel

 They weren't very bright, but they were very, very big. Ad contrast: free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter