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

Formatter and String.format ignore the width with the percent modifier (%5%)

    Details

    • Subcomponent:
    • Resolved In Build:
      b18
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        Description

        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


          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  sherman Xueming Shen (Inactive)
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  3 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: