The following test case:

  public class StringTest {
    public static void main(String[] args) {
      System.out.println(String.format("%08x", 1234));
    }
  }

produces the following error:

Exception in thread "main" java.lang.NumberFormatException: invalid character 
at position 2 in 08
   at java.lang.Integer.parseInt(Integer.java:837)
   at java.lang.Integer.decode(Integer.java:568)
   at java.util.Formatter.parseInt(Formatter.java:1191)
   at java.util.Formatter.parseArgumentIndex(Formatter.java:1212)
   at java.util.Formatter.format(Formatter.java:1326)
   at java.util.Formatter.format(Formatter.java:1442)
   at java.lang.String.format(String.java:1984)
   at java.lang.String.format(String.java:1990)
   at StringTest.main(StringTest.java:3)

This patch fixes the issue by switching to Integer.parseInt() in
java.util.Formatter.parseInt().

Signed-off-by: Pekka Enberg <penb...@kernel.org>
---
 ChangeLog                |    7 +++++++
 java/util/Formatter.java |    2 +-
 2 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a575fb9..f1c7c31 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-02-16  Pekka Enberg  <penb...@kernel.org>
+
+       * java/util/Formatter.java:
+       (parseInt): Use Integer.parseInt() insted of Integer.decode()
+       because the latter doesn't work with leading zeros which are
+       used in String.format() formatting, for example.
+
 2010-07-02  Ivan Maidanski  <iv...@mail.ru>
 
        * java/util/regex/Pattern.java:
diff --git a/java/util/Formatter.java b/java/util/Formatter.java
index 04ae805..62f6845 100644
--- a/java/util/Formatter.java
+++ b/java/util/Formatter.java
@@ -1188,7 +1188,7 @@ public final class Formatter
       advance();
     if (start == index)
       return -1;
-    return Integer.decode(format.substring(start, index));
+    return Integer.parseInt(format.substring(start, index));
   }
 
   /**
-- 
1.7.1


Reply via email to