我正在进行从字符串到 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);
}
}
关于java - 获取数字格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39486484/