【问题标题】:Getting number format exception [duplicate]获取数字格式异常[重复]
【发布时间】:2016-09-14 09:04:00
【问题描述】:

我正在做从字符串到整数的简单转换,但出现数字格式异常:

我用过下面的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)

为什么会出现上述异常?

【问题讨论】:

  • 超出范围。
  • 一个int的最大值在20亿左右。 70 亿太大了。
  • 因为值大于整数范围 int:32 位整数值范围从-2147483648 到 2147483647

标签: java


【解决方案1】:

那是因为它超出了整数范围。 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

source


改用“长”数据类型

public class HelloWorld
{
  public static void main(String[] args)
  {
    String cId = "7000000141";
    long iCid = Long.parseLong(cId);
    System.out.println(iCid);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多