Why Integer.getInteger does not work?

前端 未结 5 1787
你的背包
你的背包 2021-01-01 14:34

I have the following code:

game.log.fine(\"HERE\" + bestMove.get(\"score\"));
Integer bestScore = Integer.getInteger(bestMove.get(\"score\"));
game.log.fine(         


        
相关标签:
5条回答
  • 2021-01-01 15:11

    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

    0 讨论(0)
  • 2021-01-01 15:15

    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))
    
    0 讨论(0)
  • 2021-01-01 15:20

    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.

    0 讨论(0)
  • 2021-01-01 15:22

    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.

    0 讨论(0)
  • 2021-01-01 15:29

    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.

    0 讨论(0)
提交回复
热议问题