473,465 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why is it?about Integer

2 New Member
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
3 2192
r035198x
13,262 MVP
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
horace1
1,510 Recognized Expert Top Contributor
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
maverick19
25 New Member
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

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

Similar topics

2
by: wesley1970 | last post by:
In apache, i set override all, open rewrite mode.. I have installed wordpress into it. It can rewrite url. That means my setting is correct. Look at wordpress htaccess, something like this ...
1
by: bill12 | last post by:
What is Real Estate Agent?Where to find a Solution or service about it?please help me!I'm waiting! Thanks! who can do it with java?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.