Java Integer.parseInt 数字格式异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20219894/
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
Integer.parseInt number format exception?
提问by Danny
I feel like I must be missing something simple, but I am getting a NumberFormatException
on the following code:
我觉得我一定错过了一些简单的东西,但我得到了NumberFormatException
以下代码:
System.out.println(Integer.parseInt("howareyou",35))
It can convert the String yellow
from base 35, I don't understand why I would get a NumberFormatException
on this String.
它可以yellow
从 base 35转换字符串,我不明白为什么我会NumberFormatException
在这个字符串上得到一个。
采纳答案by René Link
Because the result will get greater than Integer.MAX_VALUE
因为结果会大于Integer.MAX_VALUE
Try this
尝试这个
System.out.println(Integer.parseInt("yellow", 35));
System.out.println(Long.parseLong("howareyou", 35));
and for
并为
Long.parseLong("abcdefghijklmno",25)
you need BigInteger
你需要大整数
Try this and you will see why
试试这个,你就会明白为什么
System.out.println(Long.MAX_VALUE);
System.out.println(new BigInteger("abcdefghijklmno",25));
回答by Glenn Teitelbaum
From the JavaDocs:
来自 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. FALSE: "howareyou" is notnull
and over 0 length- The radix is either smaller than
Character.MIN_RADIX
or larger thanCharacter.MAX_RADIX
. FALSE: 35 is in range [2,36]- 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. FALSE: all characters of "howareyou" are in radix range [0,'y']
- ==>The value represented by the string is not a value of type
int
. TRUE: The reason for the exception. The value is too large for anint
.
NumberFormatException
如果发生以下任何一种情况,则抛出类型异常:
- 第一个参数是
null
或是长度为零的字符串。错误:“howeyou”不是null
并且超过 0 长度- 基数小于
Character.MIN_RADIX
或大于Character.MAX_RADIX
。错误:35 在范围内 [2,36]- 字符串的任何字符都不是指定基数的数字,但第一个字符可以是减号“-”('\u002D')或加号“+”('\u002B'),前提是该字符串是长于长度 1。错误:“howareyou”的所有字符都在基数范围内 [0,'y']
- ==>字符串表示的值不是类型的值
int
。TRUE:异常的原因。该值对于int
.
Either Long
or BigInteger
should be used
无论是Long
或BigInteger
应当使用
回答by Blub
Could it be that the number is > Integer.MAX_VALUE
? If I try your code with Long
instead, it works.
难道数字是 > Integer.MAX_VALUE
?如果我尝试使用您的代码Long
,它会起作用。
回答by christopher
As you can see, you're running out of space in your Integer
. By swapping it out for a Long
, you get the desired result. Here is the IDEOne Link to the working code.
如您所见,您的Integer
. 通过将其替换为 a Long
,您可以获得所需的结果。这是工作代码的 IDEOne 链接。
Code
代码
System.out.println(Integer.parseInt("YELLOW",35));
System.out.println(Long.parseLong("HOWAREYOU",35));
回答by Rahul Tripathi
The number is getting bigger than Integer.MAX_VALUE
数字越来越大于Integer.MAX_VALUE
Try this:
尝试这个:
System.out.println(Integer.parseInt("yellow", 35));
System.out.println(Long.parseLong("howareyou", 35));
As seen in René Link comments you are looking for something like this using a BigInteger:
正如 René Link 评论中所见,您正在使用BigInteger寻找类似的东西:
BigInteger big=new BigInteger("abcdefghijklmno", 25);
Something like this:
像这样的东西:
System.out.println(Long.MAX_VALUE);
System.out.println(new BigInteger("abcdefghijklmno",25));
回答by Bob Flannigon
The number produced is too large for a Java Integer, use a Long.
生成的数字对于 Java 整数来说太大,请使用 Long。
回答by Joe
The previous answers of parseLong would be correct, but sometime that is also not large enough so the other option would to use a BigInteger.
parseLong 的先前答案是正确的,但有时也不够大,因此另一个选项将使用 BigInteger。
Long.parseLong("howareyou", 35)
new BigInteger("howareyou", 35)