Question regarding java.util.Formatter width
Brandon Passanisi
brandon.passanisi at oracle.com
Tue Nov 22 00:13:01 UTC 2011
Hello core-libs-dev. I am currently investigating the issues regarding
the following bug:
http://monaco.sfbay.sun.com/detail.jsf?cr=6178739
I have spent a good amount of time stepping through the simple
TestFormat code supplied in the bug report. I noticed some interesting
behavior when running the following line in the test which throws the
MissingFormatWidthException:
System.out.printf("%0.4f\n", 56789.456789);
During the execution of this line, the Formatter class appears to treat
the "0" not as a width value, which the author of the bug report
intended, but rather as a flag value. "0" is a valid flag value
indicating that the result will be zero-padded. But, in this case the
author of the bug report intended to use the "0" as a width value,
citing the Formatter specification text "The optional width is a
non-negative decimal integer indicating the minimum number of characters
to be written to the output.". Using the "0" in this manner results in
a MissingFormatWidthException. Since "0" can be a flag value and also a
width value, I am wondering if we have found some sort of gray area in
the specification.
So, my questions are:
1. Is there possibly some specification text that needs to be used
to disallow "0" for a width value?
2. Or, is a value of "0" allowed to be used for both a flag value
and width value?
3. If question #2 is true, can "0" be used for both a flag and a
width value at the same time? I ask this because the current
behavior appears to not work when running the following line:
System.out.printf("%00.4f\n", 56789.456789);
run:
Exception in thread "main"
java.util.DuplicateFormatFlagsException: Flags = '0'
at java.util.Formatter$Flags.parse(Formatter.java:4186)
at
java.util.Formatter$FormatSpecifier.flags(Formatter.java:2595)
at
java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2664)
at java.util.Formatter.parse(Formatter.java:2528)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at
formatterwidthzero.FormatterWidthZero.main(FormatterWidthZero.java:18)
Thanks.
--
Oracle <http://www.oracle.com>
Brandon Passanisi | Principle Member of Technical Staff
Oracle Java Standards Conformance
Green Oracle <http://www.oracle.com/commitment> Oracle is committed to
developing practices and products that help protect the environment
More information about the core-libs-dev
mailing list