【问题标题】:java formatting tabular output using picoclijava使用picocli格式化表格输出
【发布时间】:2021-12-19 22:10:05
【问题描述】:

我正在寻找格式化此代码:

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]);
        }

这是早期堆栈交换问题 (java formatting tabular output) 的衍生示例

但是,使用我的标记字符串,而不是正确获取tabbed headers, 我收到的是this

发生了什么事?

【问题讨论】:

  • 图片的链接是否正确? “正确的选项卡标题”链接实际上是不正确的输出吗?
  • 哎呀!对,那是正确的。让我编辑帖子。 @吉姆加里森

标签: java format picocli


【解决方案1】:

奇怪,我觉得不错。

代码:

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              

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多