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

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

 



With the following testcase (Test.java attached), the output of jamvm is:
-42

It should have thrown an exception.

I attached a patch to correct Integer.java. I could commit it, but I can't find my username/password. Can someone commit it?

(I already wrote to classpath-patches, but I may have a bigger audience here)

Thanks,
Nicolas
public class Test {
  public static void main(String[] args) {
    System.out.println(Integer.parseInt("+42"));
  }
}
--- Integer.java	2008-05-03 19:01:55.000000000 +0200
+++ Integer.java	2008-05-03 19:02:53.000000000 +0200
@@ -776,13 +776,12 @@
     if (len == 0)
       throw new NumberFormatException("string length is null");
     int ch = str.charAt(index);
-    if (ch == '-' || ch == '+')
+    if (ch == '+')
+      throw new NumberFormatException("For input string " + str);
+    if (ch == '-')
       {
         if (len == 1)
-          if (ch == '-')
-            throw new NumberFormatException("pure '-'");
-          else if (ch == '+')
-            throw new NumberFormatException("pure '+'");
+          throw new NumberFormatException("pure '-'");
         isNeg = true;
         ch = str.charAt(++index);
       }

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

  Powered by Linux