Forum Stats

  • 3,874,184 Users
  • 2,266,680 Discussions
  • 7,911,761 Comments

Discussions

Why Integer.valueOf(String) does not recognize plus sign

941745
941745 Member Posts: 3
The Integer.valueOf(String) works fine with strings like "-1", but does not work with "+1". The following code throws NumberFormatException:

int i = Integer.valueOf( "+1" );

Does anybody know what's the reason for this?

Thanks,
-- LK

Answers

  • 936584
    936584 Member Posts: 13
    edited Jun 5, 2012 11:37AM
    Hi,

    What's the reason to use "+1" instead of "1"?

    While we need the "-" symbol to work with negative numbers, we don't need a "+" symbol to work with positive numbers, maybe that's why the compiler is complaining.

    Edited by: 933581 on Jun 5, 2012 8:36 AM
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Jun 5, 2012 11:39AM
    Why do you need a reason? it doesn't work. If the original developer of the code behind valueOf() would drop in and explain to you exactly why (which will likely be in the area of "oops"), that still doesn't change a thing: you can't do it.

    Of course you could dig a little deeper and know that valueOf actually uses parseInt() so it is in fact parseInt() which does not like parsing numbers which start with a '+' sign. But still, you aren't going to get anywhere looking for a reason. You'd better focus on searching for an alternative way of dealing with it.
    maybe that's why the compiler is complaining.
    Nobody said anything about a compiler complaining.
  • 941745
    941745 Member Posts: 3
    edited Jun 5, 2012 11:46AM
    Well, there could be different reasons for using "+1" instead of just "1"; for instance, the need to read/parse data, like temperature, produced by a third party application. It's not a problem to just ignore '+' sign when reading the data, but it still does not answer the question.

    Thanks,
    -- LK

    Edited by: user13471201 on Jun 5, 2012 8:45 AM

    Edited by: user13471201 on Jun 5, 2012 8:46 AM
  • Unknown
    edited Jun 5, 2012 11:47AM
    Welcome to the forum!
    >
    The Integer.valueOf(String) works fine with strings like "-1", but does not work with "+1". The following code throws NumberFormatException:

    int i = Integer.valueOf( "+1" );

    Does anybody know what's the reason for this?
    >
    The short answer is because it is defined that way.

    The place to start for questions like this is the Javadoc.

    You don't state what Java version you are using but for 1.6 the 'valueOf' method says this
    http://127.0.0.1:8082/resource/jar%3Afile%3A/C%3A/Software/Java/jdk1.6.0_02/jdk-6-doc.zip%21/docs/api/java/lang/Integer.html#valueOf(java.lang.String)
    >
    valueOf
    public static Integer valueOf(String s)
    throws NumberFormatExceptionReturns 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.
    >
    As you can see the description further refers you to the 'parseInt' method where you will find the answer.
    >
    parseInt
    public static int parseInt(String s)
    throws NumberFormatExceptionParses 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.
    >
    You may have to do a little digging but when you have a question like this you should read the relevant Javadoc sections, search the forum for similar questions, search on the net and then if you still can't figure it out post a thread.
  • 941745
    941745 Member Posts: 3
    I believe the decision to NOT support leading '+' sign in integer-string is a kind of weird, taking into account that leading '+' sign is supported in Double.valueOf(String). The answer like "by definition" is a good one, but this way we are not going anywhere.

    Thanks anyway,
    -- LK
This discussion has been closed.