20

I need to output data into a console as a table. I was wondering maybe there are some java libraries that would take care of drawing tables in ASCII art, aligning values inside cells, etc?

 ╔══════╤═══════════╤════════╗
 ║  ID  │ Name      │  Age   ║ 
 ╠══════╪═══════════╪════════╣
 ║  1   │ John      │   24   ║ 
 ╟──────┼───────────┼────────╢
 ║  2   │ Jeff      │   19   ║ 
 ╟──────┼───────────┼────────╢
 ║  3   │ Joel      │   42   ║ 
 ╚══════╧═══════════╧════════╝
serg
  • 106,723
  • 76
  • 306
  • 327
  • 4
    If you are serious about the *ASCII* in ASCII art, then you can only use `+`, `-` and `|` as line drawing characters, not the fancy ones you used in the example. – Roland Illig Apr 09 '11 at 23:35
  • I once created such a function in PHP (to format a table for e-mail output). It had the same interface as other functions outputting to HTML or PDF. – Paŭlo Ebermann Apr 10 '11 at 00:31
  • Here is [the link](http://svn.berlios.de/wsvn/aligilo/programo/iloj/iloj_kotizo_formatado.php) - class TekstaKotizoFormatilo. But it is not much commented, and in Esperanto, and for our own format of table, so it might not help you here. If no suitable library is found here, I might think about porting it to Java. – Paŭlo Ebermann Apr 10 '11 at 00:41
  • 1
    Hmm... how come Google Translate doesn't support Esperanto? – Vladimir Dyuzhev Apr 10 '11 at 02:15
  • 1
    The 1980's just called... they want their character set back. – WhiteFang34 Apr 10 '11 at 02:34
  • https://github.com/MarounMaroun/simple-table – Maroun Jan 19 '17 at 07:33

5 Answers5

7

This worked pretty well for me: http://code.google.com/p/java-ascii-table/

String [] header = {
      "User Name", 
      "Salary", "Designation",
      "Address", "Lucky#"
};

String[][] data = {
      { "Ram", "2000", "Manager", "#99, Silk board", "1111"  },
      { "Sri", "12000", "Developer", "BTM Layout", "22222" },
      { "Prasad", "42000", "Lead", "#66, Viaya Bank Layout", "333333" },
      { "Anu", "132000", "QA", "#22, Vizag", "4444444" },
      { "Sai", "62000", "Developer", "#3-3, Kakinada"  },
      { "Venkat", "2000", "Manager"   },
      { "Raj", "62000"},
      { "BTC"},
};

Which renders the following:

+-----------+--------+-------------+------------------------+---------+
| User Name | Salary | Designation |         Address        |  Lucky# |
+-----------+--------+-------------+------------------------+---------+
|       Ram |   2000 |     Manager |        #99, Silk board |    1111 |
|       Sri |  12000 |   Developer |             BTM Layout |   22222 |
|    Prasad |  42000 |        Lead | #66, Viaya Bank Layout |  333333 |
|       Anu | 132000 |          QA |             #22, Vizag | 4444444 |
|       Sai |  62000 |   Developer |         #3-3, Kakinada |         |
|    Venkat |   2000 |     Manager |                        |         |
|       Raj |  62000 |             |                        |         |
|       BTC |        |             |                        |         |
+-----------+--------+-------------+------------------------+---------+
Mike Valenty
  • 8,765
  • 2
  • 28
  • 32
4

Try iNamik Text Table Formatter for Java.

Matt Ball
  • 344,413
  • 96
  • 627
  • 693
Jonas Kongslund
  • 4,830
  • 2
  • 27
  • 27
3

I like your table and wrote that: https://github.com/klaus31/ascii-art-table

user470370
  • 570
  • 1
  • 5
  • 17
3

Here is also a handy library: https://github.com/JakeWharton/flip-tables

As the doc said:

String[] headers = { "Test", "Header" };
String[][] data = {
    { "Foo", "Bar" },
    { "Kit", "Kat" },
};
System.out.println(FlipTable.of(headers, data));

should have the following output:

╔══════╤════════╗
║ Test │ Header ║
╠══════╪════════╣    
║ Foo  │ Bar    ║
╟──────┼────────╢
║ Kit  │ Kat    ║
╚══════╧════════╝
zhxchen17
  • 1,537
  • 2
  • 15
  • 23
0

If you already have a formatted 2d array of strings with the desired column widths, then you can draw a simple table yourself without any libraries, something like this:

+------+---------+-------+
|  ID  |  Name   |  Age  |
+------+---------+-------+
|   1  |  John   |   24  |
+------+---------+-------+
|   2  |  Jeff   |   19  |
+------+---------+-------+
|   3  |  Joel   |   42  |
+------+---------+-------+

Try it online!

public static String drawTable(String[][] table) {
    String borderRow = Arrays.stream(table[0])
            // border row between rows
            .map(str -> "-".repeat(str.length()))
            .collect(Collectors.joining("+", "+", "+\n"));
    return Arrays.stream(table)
            // table row with borders between cells
            .map(row -> Arrays.stream(row)
                    .collect(Collectors.joining("|", "|", "|\n")))
            .collect(Collectors.joining(borderRow, borderRow, borderRow));
}
public static void main(String[] args) {
    String[][] table = {
            {"  ID  ", "  Name   ", "  Age  "},
            {"   1  ", "  John   ", "   24  "},
            {"   2  ", "  Jeff   ", "   19  "},
            {"   3  ", "  Joel   ", "   42  "}};

    System.out.println(drawTable(table));
}

See also:
How to draw a staircase with Java?
Formatting 2d array of numbers