JDK-6469160 : (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 6,7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2006-09-11
  • Updated: 2014-04-16
  • Resolved: 2013-06-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b98Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

EXTRA RELEVANT SYSTEM CONFIGURATION :
Using Eclipse 3.2

A DESCRIPTION OF THE PROBLEM :
The following code produces an ArrayIndexOutOfBoundsException:

import java.util.Formatter

...
 
    Formatter f = new Formatter();
    f.format("%.1g", 0.0); // <- exception thrown here

...

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the above code snippet...

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No exception thrown...
ACTUAL -
Exception thrown

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:3356)
	at java.util.Formatter$FormatSpecifier.print(Formatter.java:3250)
	at java.util.Formatter$FormatSpecifier.print(Formatter.java:3145)
	at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2717)
	at java.util.Formatter$FormatSpecifier.print(Formatter.java:2666)
	at java.util.Formatter.format(Formatter.java:2432)
	at java.util.Formatter.format(Formatter.java:2366)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Formatter

public class App {
 
    public static void main(String args[]) {

       Formatter f = new Formatter();
       f.format("%.1g", 0.0);

    }

}

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

CUSTOMER SUBMITTED WORKAROUND :
Use any of the following for the format string:

"%.2g"
"%.1f"
"%.1e"

They all work, but obviously are not exact workarounds...