Java Integer.parseInt failed to parse string

I am parsing a 15-digit string as follows:

String str = "100004159312045"; int i = Integer.parseInt(str); 

I get an exception when doing this. What for? What are the limitations for Integer.parseInt ? What other options are there for converting such a long string to a number?

+6
source share
5 answers

Your number is too big to match an int , which is 32 bits and only has a range from -2,147,483,648 to 2,147,483,647.

Try Long.parseLong . A long has 64 bits and has a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

+24
source

int - 32-bit bit, its maximum value is 2 ^ 31-1 = 2147483647

+4
source

because int maximum value is slightly over 2,000,000,000

you can use long or BigInteger

long has a double digit that it can store (the maximum value is square than the value of int), and BigInteger can handle arbitrarily large numbers

+4
source

The value you are trying to analyze exceeds the maximum size of Integer.

+3
source

The range int is between Integer.MAX_VALUE and Integer.MIN_VALUE inclusive (i.e. from 2147483647 to -2147483648). You cannot parse the int value of this range.

You can use long m = Long.parseLong(str); or BigInteger b = new BigInteger(str); for a big whole.

+3
source

Source: https://habr.com/ru/post/900901/


All Articles