的Integer.parseInt(QUOT; 9999999990&QUOT); [英] Integer.parseInt("9999999990");

查看:109
本文介绍了的Integer.parseInt(QUOT; 9999999990&QUOT);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获得String 9999999990的NumberFormatException,但是当我在该String上调用Integer.parseInt时,我使用1111111110

让我知道是错的。

I am getting NumberFormatException for the String 9999999990 but not when I use 1111111110 when I call Integer.parseInt over that String. Let me know what is the wrong.

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


推荐答案

Integer.parseInt 将抛出异常,当它解析不能被表示为一个 int 。第一个例子是近100亿,大于最大可能的 int ,这超过了20亿。

Integer.parseInt will throw an exception when what it's parsing can't be represented as an int. The first example is almost 10 billion, which is larger than the largest possible int, which is a little over 2 billion.

Integer.parseInt(String)委托给Integer.parseInt(String,10) ,需要一个基数的版本,那些Javadoc状态:

Integer.parseInt(String) delegates to Integer.parseInt(String, 10), the version that takes a radix, and those Javadocs state:


如果出现以下任何一种情况,则会抛出NumberFormatException类型的异常:

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


  • 第一个参数为null或是长度为零的字符串。

  • 基数小于Character.MIN_RADIX或大于Character.MAX_RADIX。

  • 字符串的任何字符不是指定基数的数字,除了第一个字符可能是减号 - ('\\\-')或加号'+'('\\\+'),前提是该字符串长于长度1。 li>
  • 由字符串表示的值为不是int类型的值。

  • 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 ,这将处理更大的数字。

If you need it parsed, you can use Long.parseLong, which will handle larger numbers.

这篇关于的Integer.parseInt(QUOT; 9999999990&QUOT);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆