-
Bug
-
Resolution: Fixed
-
P4
-
8, 9, 10, 11
-
b18
-
generic
-
generic
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8205254 | 11.0.1 | Xueming Shen | P4 | Resolved | Fixed | team |
ADDITIONAL SYSTEM INFORMATION :
Observable both with Java 8 and 11 EA:
$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
$ java -version
java version "11-ea" 2018-09-25
Java(TM) SE Runtime Environment 18.9 (build 11-ea+15)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11-ea+15, mixed mode)
A DESCRIPTION OF THE PROBLEM :
java.util.Formatter (and hence String.format) accepts a 'width' field for the `%` modifier, together with an optional `-` flag for left alignment, but ignores both.
For example, the format `"~~%5%~~"` should render as `"~~ %~~"` but renders as just `"~~%~~"` without the spaces.
The Javadoc specifically requests the spaces at https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#dper
Either the implementation or the Javadoc is erroneous. Arguably, a third reasonable specification *and* implementation would be to throw a `java.util.IllegalFormatWidthException` exception instead, as is the case for the `%5n` modifier.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the `Test.java` file provided below.
$ javac Test.java
$ java Test
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
~~ %~~
~~% ~~
~~ %~~
~~% ~~
(with the appropriate spaces)
ACTUAL -
~~%~~
~~%~~
~~%~~
~~%~~
(without the appropriate spaces)
---------- BEGIN SOURCE ----------
// Test.java
import java.util.Formatter;
public class Test {
public static void main(String[] args) {
// Expected: "~~ %~~", actual: "~~%~~"
System.out.println(String.format("~~%5%~~", 5));
// Expected: "~~% ~~", actual: "~~%~~"
System.out.println(String.format("~~%-5%~~", 5));
// Expected: "~~ %~~", actual: "~~%~~"
System.out.println(new Formatter().format("~~%5%~~", 5).toString());
// Expected: "~~% ~~", actual: "~~%~~"
System.out.println(new Formatter().format("~~%-5%~~", 5).toString());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Write out the spaces in the format string instead:
String.format("~~ %%~~")
String.format("~~%% ~~")
FREQUENCY : always
Observable both with Java 8 and 11 EA:
$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
$ java -version
java version "11-ea" 2018-09-25
Java(TM) SE Runtime Environment 18.9 (build 11-ea+15)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11-ea+15, mixed mode)
A DESCRIPTION OF THE PROBLEM :
java.util.Formatter (and hence String.format) accepts a 'width' field for the `%` modifier, together with an optional `-` flag for left alignment, but ignores both.
For example, the format `"~~%5%~~"` should render as `"~~ %~~"` but renders as just `"~~%~~"` without the spaces.
The Javadoc specifically requests the spaces at https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#dper
Either the implementation or the Javadoc is erroneous. Arguably, a third reasonable specification *and* implementation would be to throw a `java.util.IllegalFormatWidthException` exception instead, as is the case for the `%5n` modifier.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the `Test.java` file provided below.
$ javac Test.java
$ java Test
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
~~ %~~
~~% ~~
~~ %~~
~~% ~~
(with the appropriate spaces)
ACTUAL -
~~%~~
~~%~~
~~%~~
~~%~~
(without the appropriate spaces)
---------- BEGIN SOURCE ----------
// Test.java
import java.util.Formatter;
public class Test {
public static void main(String[] args) {
// Expected: "~~ %~~", actual: "~~%~~"
System.out.println(String.format("~~%5%~~", 5));
// Expected: "~~% ~~", actual: "~~%~~"
System.out.println(String.format("~~%-5%~~", 5));
// Expected: "~~ %~~", actual: "~~%~~"
System.out.println(new Formatter().format("~~%5%~~", 5).toString());
// Expected: "~~% ~~", actual: "~~%~~"
System.out.println(new Formatter().format("~~%-5%~~", 5).toString());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Write out the spaces in the format string instead:
String.format("~~ %%~~")
String.format("~~%% ~~")
FREQUENCY : always
- backported by
-
JDK-8205254 Formatter and String.format ignore the width with the percent modifier (%5%)
-
- Resolved
-