Integer.parse(String str) java.lang.NumberFormatException: 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31846838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:14:20  来源:igfitidea点击:

Integer.parse(String str) java.lang.NumberFormatException: Errors

javastringexceptionintegernumberformatexception

提问by manic bubble

I keep getting number format expectations, even though I'm trimming the strings and they don't contain non numerical characters bizarrely it works for some numbers and not others. Below is an example of a string I get number format exception for. Also, any string starting with 0 e.g "0208405223", is returned 208405223, there's no zero anymore is that supposed to happen?

我不断得到数字格式的期望,即使我正在修剪字符串并且它们不包含非数字字符,它奇怪地适用于某些数字而不是其他数字。下面是我得到数字格式异常的字符串示例。此外,任何以 0 开头的字符串,例如“0208405223”,都会返回 208405223,这应该不会再出现零了吗?

String n="3020857508";
Integer a = Integer.parseInt(n.trim());
System.out.println(a);

This is the exception:

这是例外:

Exception in thread "main" java.lang.NumberFormatException: For input string: "3020857508"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)
    at JavaBeans.Main.main(Main.java:15)

回答by Bohemian

The largest number parseable as an intis 2147483647(231-1), and the largest longis 9223372036854775807(263-1), only about twice as long.

可解析为 an 的最大数int2147483647(2 31-1),最大的long9223372036854775807(2 63-1),只有大约两倍长。

To parse arbitrarily long numbers, use:

要解析任意长的数字,请使用:

import java.math.BigInteger;

BigInteger number = new BigInteger(str);

回答by ntalbs

It's because 3020857508exceeds Integer.MAX_VALUE. You should use longto convert the string to number.

那是因为3020857508超过了Integer.MAX_VALUE。您应该使用long将字符串转换为数字。

java> String n="3020857508";
//=> java.lang.String n = "3020857508"
java> Integer a = Integer.parseInt(n.trim());
//=> java.lang.NumberFormatException: For input string: "3020857508"
java> Integer.MAX_VALUE
//=> java.lang.Integer res2 = 2147483647
java> Long a = Long.parseLong(n.trim());
//=>java.lang.Long a = 3020857508

The above is javareploutput.

以上是javarepl的输出。

If you are using JDK9 or above, you can see the same result in jshell.

如果你使用的是JDK9或更高版本,你可以在jshell中看到同样的结果。

jshell> String n="3020857508"
n ==> "3020857508"

jshell> Integer a = Integer.parseInt(n.trim())
|  Exception java.lang.NumberFormatException: For input string: "3020857508"
|        at NumberFormatException.forInputString (NumberFormatException.java:65)
|        at Integer.parseInt (Integer.java:652)
|        at Integer.parseInt (Integer.java:770)
|        at (#2:1)

jshell> Integer.MAX_VALUE
 ==> 2147483647

jshell> Long a = Long.parseLong(n.trim());
a ==> 3020857508

回答by alfasin

MAX_INT is 2147483647 and you're trying to parse a bigger number as Integer.

MAX_INT 是 2147483647,您试图将更大的数字解析为整数。

You can use Long.parseLonginstead:

您可以Long.parseLong改用:

System.out.println(Long.parseLong("3020857508")); // 3020857508

回答by swapy

    String n="3020857508";
    Long l = Long.parseLong(n.trim());
    System.out.println(l);

Integer MAX_VALUE : 2147483647, Long MAX_VALUE : 9223372036854775807

整数 MAX_VALUE :2147483647,长 MAX_VALUE :9223372036854775807

As string n will not fit in Integerafter conversion we have to use Long.

由于字符串 nInteger在转换后不适合,我们必须使用Long.

And if you are still not sure about range that can come in String. go with BigIntegerwhere number is held in an int[]

如果您仍然不确定字符串中可以出现的范围。与BigInteger号码所在的地方一起去int[]

回答by Piyush Mittal

In Java, the integer range is from -2,147,483,648 to 2,147,483,647 make sure your numeric value is between the same.

在 Java 中,整数范围是从 -2,147,483,648 到 2,147,483,647 确保您的数值介于两者之间。

回答by Shar1er80

The number you're trying to parse is larger than the max value of an Integer, you would have to use a larger data type like Long.

您尝试解析的数字大于 an 的最大值Integer,您必须使用更大的数据类型,如Long.

public static void main(String[] args) {
    System.out.println("Integer max value: " + Integer.MAX_VALUE);
    System.out.println("Long max value: " + Long.MAX_VALUE);
    System.out.println();

    String n = "3020857508";
    Long a = Long.parseLong(n.trim());
    System.out.println(a);
}

Results:

结果:

Integer max value: 2147483647
Long max value: 9223372036854775807

3020857508

回答by Harshit Shrivastava

You could use

你可以用

String n="3020857508";
Long a = Long.valueOf(n.trim()).longValue();
System.out.println(a);

回答by jtothebee

Just like what @Codebender said. Your value is out of range for int. Try using long/Long instead.

就像@Codebender 所说的那样。您的值超出了 int 的范围。尝试使用 long/Long 代替。