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>
.
The problem is that bestMove.get("score")
gives a string value (equal to "50"). But if try to transform to integer, I get null
.
Does anybody know what is the problem here?
getInteger(String nm, int val) method determines the integer value of the system property with the specified name. The argument val is the default value.
valueOf() returns an Integer object while Integer. parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer.
Because Integer.getInteger
is not what you're searching for. From the 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.
You want to use Integer.parseInt
I suspect that you're looking for the Integer.parseInt
method:
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:
Determines the integer value of the system property with the specified name.
I would use the Integer.valueOf(String n)
method.
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
.
Basic 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 val is the default value.
An
Integer object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.
Or
The java.lang.Integer.getInteger(String nm)
Determines the integer value of the system property with the specified name. The argument is treated as the name of a 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 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.
Note: System properties are accessible through the System.getProperty(java.lang.String)
method.
Solution to Use:( 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
must all 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.
Or
The java.lang.Integer.valueOf(String s)
returns an Integer object holding the 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 object that represents the integer value specified by the string.
In other words, this method returns an Integer object equal to the value of:
new Integer(Integer.parseInt(s))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With