By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

why is it?about Integer

P: 2
code as following
...
String s1=Integer.toBinaryString(-1073741824)
System.out.println(s1);
int i2=Integer.parseInt(s1,2);
...
the output is ...
11000000000000000000000000000000
java.lang.NumberFormatException: For input string: "11000000000000000000000000000000"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:480)
at com.main.Tester.main(Tester.java:18)

what's the matter? how to resolve it?
Dec 13 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
code as following
...
String s1=Integer.toBinaryString(-1073741824)
System.out.println(s1);
int i2=Integer.parseInt(s1,2);
...
the output is ...
11000000000000000000000000000000
java.lang.NumberFormatException: For input string: "11000000000000000000000000000000"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:480)
at com.main.Tester.main(Tester.java:18)

what's the matter? how to resolve it?
11000000000000000000000000000000 is not an int (too big)

The specs say

"An exception of type NumberFormatException is thrown if any of the following situations occurs:
  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type int. "
Dec 13 '06 #2

Expert 100+
P: 1,510
as indicated by r035198x the 32-bit binary value 11000000000000000000000000000000
is too large for an int. If you you convert it to a BigInteger, e.g.
Expand|Select|Wrap|Line Numbers
  1. String s1=Integer.toBinaryString(-1073741824);
  2. System.out.println(s1.length() + " bits " +s1);
  3. BigInteger bigI = new BigInteger(s1, 2);
  4. System.out.println("Big integer " + bigI);
  5.  
this prints
32 bits 11000000000000000000000000000000
Big integer 3221225472

showing that 3221225472 is larger than 2147483647 the maximum value an int can have
Dec 13 '06 #3

P: 25
the reasons why your int value was long because in binary representation(2s complement) the first bit is the sign bit.
Dec 13 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.