Integer.parseInt(string)给出了数字格式异常 [英] Integer.parseInt(string) is giving me number format exception
本文介绍了Integer.parseInt(string)给出了数字格式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这段代码抛出 NumberFormatException
?
int a = Integer.parseInt("1111111111111111111111111111111111111111");
如何获取 int
的值字符串
?
推荐答案
您尝试解析的值比最大允许的 int
值大得多( Integer.MAX_VALUE
或 2147483647
),所以<$ c抛出$ c> NumberFormatException 。它还大于最大允许的 long
( Long.MAX_VALUE
或 9223372036854775807L
),因此您需要一个 BigInteger
来存储该值。
The value that you're attempting to parse is much bigger than the biggest allowable int
value (Integer.MAX_VALUE
, or 2147483647
), so a NumberFormatException
is thrown. It is bigger than the biggest allowable long
also (Long.MAX_VALUE
, or 9223372036854775807L
), so you'll need a BigInteger
to store that value.
BigInteger veryBig = new BigInteger("1111111111111111111111111111111111111111");
来自 BigInteger
Javadocs :
From BigInteger
Javadocs:
不可变的任意精度整数。
Immutable arbitrary-precision integers.
这篇关于Integer.parseInt(string)给出了数字格式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文