2

I'm looking to format this code:

Object[][] table = new String[4][];
        table[0] = new String[] { "Pie", "2.2", "12" };
        table[1] = new String[] { "Cracker", "4", "15" };
        table[2] = new String[] { "Pop tarts", "1", "4" };
        table[3] = new String[] { "Sun Chips", "5", "2" };

        String itemId = CommandLine.Help.Ansi.AUTO.string("@|bold,green,underline ItemId|@");

        System.out.format("%-15s%-15s%-15s%-15s\n", itemId, "Item", "Price", "Quantity");
        for (int i = 0; i < table.length; i++) {
            Object[] row = table[i];
            System.out.format("%-15d%-15s%-15s%-15s\n", i, row[0], row[1], row[2]);
        }

This is a spin-off example from an earlier stack exchange question (java formatting tabular output)

However, with my markup string, rather than getting properly tabbed headers, I'm getting this instead.

What's going on?

2
  • Are the links to the images correct? Is the "properly tabbed headers" link actually the incorrect output? Commented Nov 5, 2021 at 23:37
  • Whoops! Yes, that's correct. Let me edit the post. @JimGarrison Commented Nov 8, 2021 at 15:10

1 Answer 1

2

strange, looks good to me.

code:

import picocli.CommandLine; 
public static void main(String... args) {
        Object[][] table = new String[4][];
        table[0] = new String[]{"Pie", "2.2", "12"};
        table[1] = new String[]{"Cracker", "4", "15"};
        table[2] = new String[]{"Pop tarts", "1", "4"};
        table[3] = new String[]{"Sun Chips", "5", "2"};

        String itemId = CommandLine.Help.Ansi.AUTO.string("@|bold,green,underline ItemId|@");

        System.out.format("%-15s%-15s%-15s%-15s\n", itemId, "Item", "Price", "Quantity");
        for (int i = 0; i < table.length; i++) {
            Object[] row = table[i];
            System.out.format("%-15d%-15s%-15s%-15s\n", i, row[0], row[1], row[2]);

        }
    } ```

output: 

ItemId         Item           Price          Quantity       
0              Pie            2.2            12             
1              Cracker        4              15             
2              Pop tarts      1              4              
3              Sun Chips      5              2              
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.