获取数字格式异常
问题描述:
我从字符串做简单的转换为int,但得到的数字格式异常:获取数字格式异常
我有以下Java程序使用:
String cId = "7000000141";
int iCid = Integer.parseInt(cId);
System.out.println(iCid);
获取例外如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "7000000141"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:459)
at java.lang.Integer.parseInt(Integer.java:497)
为什么我得到上述异常?
答
这是因为它超出了整数的范围。 integer
的最大允许值为2147483647
在Java中,以下是最小值和最大值。
width minimum maximum
int: 32 bit -2 147 483 648 +2 147 483 647
long: 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
使用 '长' 数据类型,而不是
public class HelloWorld
{
public static void main(String[] args)
{
String cId = "7000000141";
long iCid = Long.parseLong(cId);
System.out.println(iCid);
}
}
这是超出范围。 –
int的最大值约为20亿。 70亿太大了。 – khelwood
它因为值大于整数范围int:32位 整数值范围从-2147483648开始到2147483647 –