Output in a table format in Java's System.out

前端 未结 8 1447
清歌不尽
清歌不尽 2020-11-22 15:03

I\'m getting results from a database and want to output the data as a table in Java\'s standard output

I\'ve tried using \\t but the first column I want is very vari

8条回答
  •  鱼传尺愫
    2020-11-22 15:54

    public class Main {
     public static void main(String args[]) {
       String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
       System.out.format(format, "A", "AA", "AAA");
       System.out.format(format, "B", "", "BBBBB");
       System.out.format(format, "C", "CCCCC", "CCCCCCCC");
    
       String ex[] = { "E", "EEEEEEEEEE", "E" };
    
       System.out.format(String.format(format, (Object[]) ex));
     }
    }
    

    differece in sizes of input doesnt effect the output

提交回复
热议问题