Java Integer.parseInt failed to parse a string

后端 未结 5 944
忘了有多久
忘了有多久 2020-12-02 00:30

I\'m parsing a string of 15 digits like this:

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

I\'m getting an exception wh

相关标签:
5条回答
  • 2020-12-02 00:38

    Your number is too large to fit in an int which is 32 bits and only has a range of -2,147,483,648 to 2,147,483,647.

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

    0 讨论(0)
  • 2020-12-02 00:50

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

    0 讨论(0)
  • 2020-12-02 00:50

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

    You can use long m = Long.parseLong(str); or BigInteger b = new BigInteger(str); for large integer.

    0 讨论(0)
  • 2020-12-02 00:59

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

    0 讨论(0)
  • 2020-12-02 01:03

    because int's maximum value is a little above 2,000,000,000

    you can use long or BigInteger

    long has double the digits it can store (the max value is square that of int) and BigInteger can handle arbitrarily large numbers

    0 讨论(0)
提交回复
热议问题