22

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?

1
  • Integer.parseInteger() instead? :) Commented Dec 9, 2010 at 10:46

5 Answers 5

38

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

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant function naming.
13

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.

Comments

10

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 yield null.

4 Comments

Don't use valueOf internally it is Integer.valueOf(parseInt(s, 10))' ... where parserInt()` is nothing but Integer.parseInt(String) with radix 10
@Favonius, true, hence it's essentially the same as Integer.parseInt(s, 10). I would still suggest valueOf() since the JVM uses it for autoboxing anyways. What I'm saying is, if it's there to use, use it.
I agree the JVM uses the valueOf but that is for valueOf(int). Also the reason for using it in autoboxing is ` as it returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.` - Taken from javadoc
@Favonius, sorry...wrong example of use of valueOf(). Essentially, they are both the same (valueOf(), parseInt()), so it's user preference.
4

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.

4 Comments

the second argument to parseInt can be omitted.
@aioobe: true, but I'd use it anyway, just to be safe.
Safe about what? The default is 10. They're not going to change it on us. And there nothing ambiguous about it either.
@aioobe, @EJP: guys, you were both right. Ghosts of Javascript must have haunted me because parseInt() there will treat 034 and 34 differently; first as octal, second as decimal, if you don't explicitly specify the radix.
4

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.