Java 为什么 Integer.getInteger 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4397328/
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
Why Integer.getInteger does not work?
提问by Roman
I have the following code:
我有以下代码:
game.log.fine("HERE" + bestMove.get("score"));
Integer bestScore = Integer.getInteger(bestMove.get("score"));
game.log.fine("THERE" + bestScore);
As an output I have:
作为输出,我有:
FINE: HERE50
Dec 9, 2010 11:34:17 AM game.Agent getCloud
FINE: THEREnull
Dec 9, 2010 11:34:17 AM game.Agent getCloud
Probably I had to add that bestMove is HashMap<String,String>
.
可能我不得不补充说 bestMove 是HashMap<String,String>
.
The problem is that bestMove.get("score")
gives a string value (equal to "50"). But if try to transform to integer, I get null
.
问题是bestMove.get("score")
给出了一个字符串值(等于“50”)。但是如果尝试转换为整数,我会得到null
.
Does anybody know what is the problem here?
有人知道这里有什么问题吗?
回答by aioobe
I suspect that you're looking for the Integer.parseInt
method:
我怀疑您正在寻找该Integer.parseInt
方法:
Parses the string argument as a signed decimal integer.
将字符串参数解析为有符号十进制整数。
Example usage:
用法示例:
int bestScore = 0;
try {
bestScore = Integer.parseInt(bestMove.get("score"));
} catch (NumberFormatException nfe) {
// handle exception gracefully
}
The Integer.getInteger
does something completely different:
在Integer.getInteger
做了完全不同的:
Determines the integer value of the system property with the specified name.
确定具有指定名称的系统属性的整数值。
回答by darioo
You should use
你应该使用
Integer.parseInt
in your code since
在你的代码中
Integer.getInteger
will determine the integer value of the system property with the specified name.
将确定具有指定名称的系统属性的整数值。
Correct code would be:
正确的代码是:
Integer bestScore = Integer.parseInt(bestMove.get("score"), 10);
That 10
as the second arguments is the radix. Use it always so your number won't be parsed ambigously.
那10
作为第二个参数是基数。始终使用它,这样您的号码就不会被歧义解析。
回答by Valentin Rocher
Because Integer.getInteger
is not what you're searching for. From the Javadoc :
因为Integer.getInteger
不是你要找的。从 Javadoc :
Determines the integer value of the system property with the specified name. The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty.
If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.
确定具有指定名称的系统属性的整数值。第一个参数被视为系统属性的名称。系统属性可通过 System.getProperty(java.lang.String) 方法访问。此属性的字符串值然后被解释为整数值,并返回表示此值的 Integer 对象。可以通过 getProperty 的定义找到可能的数字格式的详细信息。
如果没有具有指定名称的属性,如果指定的名称为空或为空,或者如果该属性没有正确的数字格式,则返回空值。
You want to use Integer.parseInt
你想用 Integer.parseInt
回答by Buhake Sindi
I would use the Integer.valueOf(String n)
method.
我会用这个Integer.valueOf(String n)
方法。
Integer bestScore = Integer.valueOf(bestMove.get("score"));
From this blog, the reason they gave,
从这个博客,他们给出的原因,
Integer.getInteger(String)
converts a String to a number by assuming the String is the name of a system property numeric representation. In other words.Integer.getInteger("12345")
is likely to yieldnull
.
Integer.getInteger(String)
通过假设 String 是系统属性数字表示形式的名称,将 String 转换为数字。换句话说。Integer.getInteger("12345")
很可能屈服null
。
回答by Chandra Sekhar
Basic Integer.getInteger
:
基本Integer.getInteger
:
The java.lang.Integer.getInteger(String nm, int val)
method determines the integer value of the system property with the specified name.The argument valis the default value.
An
Integer objectthat represents the value of the second argumentis returned if there is no propertyof the specified name, if the property does not have the correct numeric format, or if the specified name is emptyor null.
该java.lang.Integer.getInteger(String nm, int val)
方法确定具有指定名称的系统属性的整数值。参数val是默认值。如果没有指定名称的属性,如果该属性没有正确的数字格式,或者如果指定的名称为空或null,则返回表示第二个参数值
的
Integer 对象。
Or
或者
The java.lang.Integer.getInteger(String nm)
Determines the integer value of the system propertywith the specified name. The argument is treated as the name ofa system property. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. If there is no propertywith the specified name, if the specified name is empty or null, or if the property does not havethe correct numeric format, then nullis returned.
该java.lang.Integer.getInteger(String nm)
决定的整数值系统属性具有指定名称。该参数被视为系统属性的名称。此属性的字符串值然后被解释为整数值,并返回表示此值的 Integer 对象。如果没有财产具有指定名称时,如果指定名称为空或null,或者属性并没有在正确的数字格式,然后空返回。
Note:System properties are accessible through the System.getProperty(java.lang.String)
method.
注意:系统属性可通过该System.getProperty(java.lang.String)
方法访问。
Solution to Use:( Integer.parseInt
/ Integer.valueOf
)
解决方案中使用:(Integer.parseInt
/ Integer.valueOf
)
The java.lang.Integer.parseInt(String s)
parses the string argument as a signed decimal integer. The characters in the string
mustall be decimal digits, except that the first character may be an ASCII
minus sign '-' ('\u002D')
to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int)
method.
在java.lang.Integer.parseInt(String s)
解析字符串参数作为有符号的十进制整数。字符串中的字符
必须都是十进制数字,除非第一个字符为ASCII
减号'-' ('\u002D')
来表示负值。返回结果整数值,就好像参数和基数 10 作为参数提供给parseInt(java.lang.String, int)
方法一样。
Or
或者
The java.lang.Integer.valueOf(String s)
returns an Integer object holdingthe value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String)
method. The result is an Integer objectthat represents the integer value specified by the string.
In other words, this method returns an Integer object equal to the value of:
在java.lang.Integer.valueOf(String s)
返回整数对象保持的值指定的字符串。参数被解释为表示一个带符号的十进制整数,就像参数被提供给parseInt(java.lang.String)
方法一样。结果是一个Integer 对象,表示由字符串指定的整数值。
换句话说,此方法返回一个等于以下值的 Integer 对象:
new Integer(Integer.parseInt(s))