# Why NumberFormatException ???

Amit Das

Ranch Hand

Posts: 206

posted 10 years ago

- 0

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

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

Ariel Ortiz

Ranch Hand

Posts: 121

posted 10 years ago

- 0

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

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

Joe Sondow

Ranch Hand

Posts: 195

posted 10 years ago

- 0

Don't confuse

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

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 ]

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

**1**1111111111111111111111111111101The 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 ]

SCJA 1.0 (98%), SCJP 1.4 (98%)

Ariel Ortiz

Ranch Hand

Posts: 121

posted 10 years ago

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

- 0

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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |