Search code examples
javastringeclipseformatting

The %[width]d formatter syntax creates a pyramid in Eclipse IDE


Using JDK 17 (Java-SE 17) This is my code:

import java.time.LocalDate;

public class TextBlock {

    public static void main(String[] args) {
        String bulletIt = "Print a Bulleted List:\n" +
                        "\t\u2022 First Point\n" +
                        "\t\t\u2022 Sub Point";
        System.out.println(bulletIt);
        
        System.out.println();
        
        String textBlock = """
                        Print a Bulleted List:
                            \u2022 First Point
                                \u2022 Sub Point""";
        
        System.out.println(textBlock);
        
        System.out.println();
        
        int age = 35;
        // .printf works the same as .format
        System.out.printf("Your age is %d%n", age);
        
        int yearOfBirth = LocalDate.now().getYear() - age;
        System.out.format("Age = %d. Birth year = %d%n", age, yearOfBirth);
        
        //System.out.printf("Your age is %f%n", (float) age);
        System.out.printf("Your age is %.2f%n", (float) age);
        
        for(int  i = 1; i <= 100_000; i *= 10) {
            System.out.format("%6d" + "\n", i);
        }
    }

}

Left out package name. Currently just learning about String formatting. The for loop at the end of my code prints this out in the console:

enter image description here

From the tutorial everything should superficially be right-aligned, but for some reason my Eclipse IDE decides to only use half of the width I defined for my conversion. Any help? I want to use the .printf more in my code and not being able to right-align text would be a serious blow. Or is this just one of the horrible tradeoffs of using Eclipse?

I tried doubling the width to 12 but again, only half of the width is being used in each conversion. I used repl.it to make sure it was a compiler issue and yea it is. Eclipse for some reason wants to be different from the crowd. Stupid Eclipse.


Solution

  • Changed the font from Ariel to a monospaced font of my choice using instructions from here; thanks to Thomas.

    Edit: Gotta wait 2 days to accept my own answer as the solution