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?

Roman
  • 124,451
  • 167
  • 349
  • 456

5 Answers5

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

Valentin Rocher
  • 11,667
  • 45
  • 59
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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 2
    Don't use `valueOf` internally it is `Integer.valueOf(parseInt(s, 10))' ... where `parserInt()` is nothing but `Integer.parseInt(String)` with radix **10** – Favonius Dec 09 '10 at 10:57
  • @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. – Buhake Sindi Dec 09 '10 at 11:03
  • 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 Dec 09 '10 at 11:15
  • @Favonius, sorry...wrong example of use of `valueOf()`. Essentially, they are both the same (`valueOf()`, `parseInt()`), so it's user preference. – Buhake Sindi Dec 09 '10 at 11:17
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))
Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
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.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • the second argument to `parseInt` can be omitted. – aioobe Dec 09 '10 at 10:51
  • @aioobe: true, but I'd use it anyway, just to be safe. – darioo Dec 09 '10 at 10:52
  • Safe about what? The default is 10. They're not going to change it on us. And there nothing ambiguous about it either. – user207421 Dec 09 '10 at 11:59
  • @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. – darioo Dec 09 '10 at 12:22