Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Integer.getInteger does not work?

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?

like image 766
Roman Avatar asked Dec 09 '10 10:12

Roman


People also ask

What is getInteger()?

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.

What is the difference between integer parseInt and integer valueOf?

valueOf() returns an Integer object while Integer. parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer.


5 Answers

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

like image 101
Valentin Rocher Avatar answered Sep 26 '22 04:09

Valentin Rocher


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.

like image 40
aioobe Avatar answered Sep 26 '22 04:09

aioobe


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.

like image 28
Buhake Sindi Avatar answered Sep 24 '22 04:09

Buhake Sindi


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))
like image 31
Chandra Sekhar Avatar answered Sep 26 '22 04:09

Chandra Sekhar


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.

like image 42
darioo Avatar answered Sep 23 '22 04:09

darioo