On 18:57 Wed 16 Feb , Pekka Enberg wrote:
> 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 <[email protected]>
> ---
> 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 <[email protected]>
> +
> + * 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 <[email protected]>
>
> * 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
>
>
The difference between the two methods is that parseInt only handles decimal
numbers
(the issue with decode is that it treats 08 as octal). As all the possible
values after % should be decimal integers (decimal integer for the argument
index,
and non-negative decimal integers for width and precision), this change seems
ok.
--
Andrew :)
Free Java Software Engineer
Red Hat, Inc. (http://www.redhat.com)
Support Free Java!
Contribute to GNU Classpath and IcedTea
http://www.gnu.org/software/classpath
http://icedtea.classpath.org
PGP Key: F5862A37 (https://keys.indymedia.org/)
Fingerprint = EA30 D855 D50F 90CD F54D 0698 0713 C3ED F586 2A37