Re: Integer.parseInt("+42") gives -42

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 12/05/2008, Andrew Haley <aph@xxxxxxxxxx> wrote:
> Andrew John Hughes wrote:
>
>  >
>  > Interestingly, our parseInt Mauve test has this:
>  >
>  >     // In JDK1.7, '+' is considered a valid character.
>  >     try
>  >       {
>  >         i = Integer.parseInt("+10");
>  >         harness.check(true);
>  >       }
>  >     catch (NumberFormatException nfe)
>  >       {
>  >         harness.fail("Leading '+' does not throw NumberFormatException");
>  >       }
>  >
>  > and indeed it does return 42 (so Classpath is still wrong returning
>  > -42).
>
>
> 1.7 Javadoc:
>
>
>     /**
>      * Parses the string argument as a signed integer in the radix
>      * specified by the second argument. The characters in the string
>      * must all be digits of the specified radix (as determined by
>      * whether {@link java.lang.Character#digit(char, int)} returns a
>      * nonnegative value), except that the first character may be an
>      * ASCII minus sign {@code '-'} (<code>'&#92;u002D'</code>) to
>      * indicate a negative value or an ASCII plus sign {@code '+'}
>      * (<code>'&#92;u002B'</code>) to indicate a positive value. The
>      * resulting integer value is returned.
>      *
>      * <p>An exception of type {@code NumberFormatException} is
>      * thrown if any of the following situations occurs:
>      * <ul>
>      * <li>The first argument is {@code null} or is a string of
>      * length zero.
>      *
>      * <li>The radix is either smaller than
>      * {@link java.lang.Character#MIN_RADIX} or
>      * larger than {@link java.lang.Character#MAX_RADIX}.
>      *
>      * <li>Any character of the string is not a digit of the specified
>      * radix, except that the first character may be a minus sign
>      * {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
>      * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
>      * string is longer than length 1.
>      *
>      * <li>The value represented by the string is not a value of type
>      * {@code int}.
>      * </ul>
>      *
>      * <p>Examples:
>      * <blockquote><pre>
>      * parseInt("0", 10) returns 0
>      * parseInt("473", 10) returns 473
>      * parseInt("+42", 10) returns 42
>

I'm just testing a patch that correctly provides the 1.7 behaviour.
I've also made Mauve spot the bad Classpath behaviour.
-- 
Andrew :-)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net

PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint: F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8


[Index of Archives]     [Linux Kernel]     [Linux Cryptography]     [Fedora]     [Fedora Directory]     [Red Hat Development]

  Powered by Linux