加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Integer.parseInt(“9999999990”);

发布时间:2020-12-15 05:12:06 所属栏目:Java 来源:网络整理
导读:参见英文答案 Unexpected NumberFormatException while parsing a hex string to an int value????????????????????????????????????5个 我得到字符串9999999990的NumberFormatException但不是我使用2222211110时 当我通过该字符串调用Integer.parseInt时.
参见英文答案 > Unexpected NumberFormatException while parsing a hex string to an int value????????????????????????????????????5个
我得到字符串9999999990的NumberFormatException但不是我使用2222211110时
当我通过该字符串调用Integer.parseInt时.
让我知道什么是错的.

String str="9999999990";
  int f = Integer.parseInt("2147483647");// No Exception here
        int x =Integer.parseInt(str);   // Exception is thrown here

解决方法

当解析它不能表示为int时,Integer.parseInt将抛出异常.第一个例子是近100亿,大于最大可能的int,这个数字略高于20亿.

Integer.parseInt(String)委托给Integer.parseInt(String,10),即带基数的版本,以及那些Javadocs状态:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix,except that the first character may be a minus sign ‘-‘ (‘u002D’) or plus sign ‘+’ (‘u002B’) provided that the string is longer than length 1.
  • The value represented by the string is not a value of type int.

(强调我的)

如果需要解析它,可以使用Long.parseLong,它将处理更大的数字.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读