Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7120291

(fmt) ArrayIndexOutOfBoundsException when formatting a zero value

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 8
    • 7
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.7.0_01"
      Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
      Java HotSpot(TM) Server VM (build 21.1-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux mizar 2.6.32-36-generic #79-Ubuntu SMP Tue Nov 8 22:29:26 UTC 2011 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      A statement like:

      System.out.println(String.format("%5.1g", v2));

      rises an exception if variable v2 is set to 0 (zero).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and run the following java program:

      package test;

      public class Test {

          public static void main(String[] args) {
              
              double v = 0.0;
              
              System.out.println(String.format("%5.1g", v));
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was expecting:

      0.0


      ACTUAL -
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at java.util.Formatter$FormatSpecifier.addZeros(Formatter.java:3401)
      at java.util.Formatter$FormatSpecifier.print(Formatter.java:3295)
      at java.util.Formatter$FormatSpecifier.print(Formatter.java:3190)
      at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2757)
      at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
      at java.util.Formatter.format(Formatter.java:2488)
      at java.util.Formatter.format(Formatter.java:2423)
      at java.lang.String.format(String.java:2845)
      at test.Test.main(Test.java:13)


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at java.util.Formatter$FormatSpecifier.addZeros(Formatter.java:3401)
      at java.util.Formatter$FormatSpecifier.print(Formatter.java:3295)
      at java.util.Formatter$FormatSpecifier.print(Formatter.java:3190)
      at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2757)
      at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
      at java.util.Formatter.format(Formatter.java:2488)
      at java.util.Formatter.format(Formatter.java:2423)
      at java.lang.String.format(String.java:2845)
      at test.Test.main(Test.java:13)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package test;

      public class Test {

          public static void main(String[] args) {
              
              double v = 0.0;
              
              System.out.println(String.format("%5.1g", v));
          }
      }

      ---------- END SOURCE ----------

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: