2013-03-05 31 views
22

我想组织信息是这样的:如何在控制台中使用ASCII创建表格?

的信息与细胞组织,而与System.out.println信息会非常混乱。

or this

+2

+1。不是一个坏问题。如果有人有这个好的图书馆,请推荐。否则,看看System.out.format – Thilo 2013-03-05 03:32:45

+1

你可以查看[this](http://stackoverflow.com/questions/15193812/how-to-print-a-table-of-arrays/15194265#15194265)示例 – MadProgrammer 2013-03-05 03:33:56

+0

您最好的投注主要是使用'System.out.printf(...)'和等效的'System.out.format(...)'。 – 2013-03-05 03:34:46

回答

38

尝试使用System.out.format()System.out.printf()printf只是调用format,所以两种方法都给出相同的结果)。

这里有一个简单的例子,它会尝试将文本对齐到左侧并用空格填充未使用的位置。将字符串左对齐可以使用%-15s来实现,这意味着为字符串(s)数据预留15位置,并从左侧开始写入(-)。如果要添加数字,请使用d后缀,如%-4d,用于放置在列左侧的最大4位数字。
顺便说一句我用%n而不是\n来表示当前操作系统使用的行分隔符序列,如Windows,它将是\r\n

你可以在Formatter class documentation找到更多的信息。

String leftAlignFormat = "| %-15s | %-4d |%n"; 

System.out.format("+-----------------+------+%n"); 
System.out.format("| Column name  | ID |%n"); 
System.out.format("+-----------------+------+%n"); 
for (int i = 0; i < 5; i++) { 
    System.out.format(leftAlignFormat, "some data" + i, i * i); 
} 
System.out.format("+-----------------+------+%n"); 

输出

+-----------------+------+ 
| Column name  | ID | 
+-----------------+------+ 
| some data0  | 0 | 
| some data1  | 1 | 
| some data2  | 4 | 
| some data3  | 9 | 
| some data4  | 16 | 
+-----------------+------+ 
+0

完美答案! ¡非常感谢! 我可以这样做,我想=) – 2013-03-08 16:20:27

+0

对于固定的要求,这个解决方案是完美的。对于更真实的生活用例,图书馆更合适。看到这个答案:http://stackoverflow.com/a/35961774/363573。 – Stephan 2016-03-12 18:45:15

5

使用System.out.printf()

例如,

String s = //Any string 
System.out.printf(%10s, s); 

将打印出字符串s的内容,占用恰好10个字符。所以如果你想要一个表格,只要确保表格中的每个单元格都打印出相同的长度。另请注意,printf()不会打印新行,因此您必须自己打印。

0

可以使用的String.Format()用正确的方法 代码可能是这个样子我猜

StringBuilder sb=new StringBuilder(); 

for(int i = 1; i <= numberOfColumns; i++) 
{ 
     sb.append(String.format(%-10s,rsMetaData.getColumnLabel(i); 
} 

由于库我不认为这有什么,会做的工作,但我可能是错误!会怎么做研究它

也有看看这个http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

0

TUIAWT,您可以在控制台窗口中使用AWT组件。但它看起来不像它支持ListTable,但它可能会给你一个起点。

+0

在撰写本文时,最近的更新到TUIAWT似乎在2015年3月。 – Stephan 2016-12-13 06:50:58

13

试试这个选择:asciitable

它提供了几个文本表的实现,最初使用ASCII和UTF-8字符作为边界。

下面是一个示例表:

 ┌──────────────────────────────────────────────────────────────────────────┐ 
    │ Table Heading               │ 
    ├──────────────────┬──────────────────┬──────────────────┬─────────────────┤ 
    │ first row (col1) │ with some  │ and more   │ even more  │ 
    │     │ information  │ information  │     │ 
    ├──────────────────┼──────────────────┼──────────────────┼─────────────────┤ 
    │ second row  │ with some  │ and more   │ even more  │ 
    │ (col1)   │ information  │ information  │     │ 
    │     │ (col2)   │ (col3)   │     │ 
    └──────────────────┴──────────────────┴──────────────────┴─────────────────┘

找到最新版本:http://mvnrepository.com/artifact/de.vandermeer/asciitable

参见: https://stackoverflow.com/a/39806611/363573

5

我的班级我这样做专门创建完全动态: https://github.com/MRebhan/crogamp/blob/master/src/com/github/mrebhan/crogamp/cli/TableList.java

您可以使用它像这样:

TableList tl = new TableList(3, "ID", "String 1", "String 2").sortBy(0).withUnicode(true); 
// from a list 
yourListOrWhatever.forEach(element -> tl.addRow(element.getID(), element.getS1(), element.getS2())); 
// or manually 
tl.addRow("Hi", "I am", "Bob"); 

tl.print(); 

它看起来像这样使用Unicode字符(注:会更好看在控制台,因为所有的字符等宽):

┌─────────┬─────────────────────────────────────────────────────────────────────────┬────────────────────────────┐ 
│ Command │ Description                │ Syntax      │ 
┢━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━┪ 
┃ bye  ┃ Quits the application.             ┃       ┃ 
┃ ga  ┃ Adds the specified game.            ┃ <id> <description> <path> ┃ 
┃ gl  ┃ Lists all currently added games           ┃ [pattern]     ┃ 
┃ gr  ┃ Rebuilds the files of the currently active game.      ┃       ┃ 
┃ gs  ┃ Selects the specified game.            ┃ <id>      ┃ 
┃ help ┃ Lists all available commands.           ┃ [pattern]     ┃ 
┃ license ┃ Displays licensing info.            ┃       ┃ 
┃ ma  ┃ Adds a mod to the currently active game.        ┃ <id> <file>    ┃ 
┃ md  ┃ Deletes the specified mod and removes all associated files.    ┃ <id>      ┃ 
┃ me  ┃ Toggles if the selected mod is active.         ┃ <id>      ┃ 
┃ ml  ┃ Lists all mods for the currently active game.       ┃ [pattern]     ┃ 
┃ mm  ┃ Moves the specified mod to the specified position in the priority list. ┃ <id> <position>   ┃ 
┃ top kek ┃ Test command. Do not use, may cause death and/or destruction   ┃       ┃ 
┃ ucode ┃ Toggles advanced unicode. (Enhanced characters)       ┃ [on|true|yes|off|false|no] ┃ 
┗━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ 

而且与unicode字符关(省略.withUnicode(true)):

Command | Description                | Syntax      
--------+-------------------------------------------------------------------------+--------------------------- 
bye  | Quits the application.             |       
ga  | Adds the specified game.            | <id> <description> <path> 
gl  | Lists all currently added games           | [pattern]     
gr  | Rebuilds the files of the currently active game.      |       
gs  | Selects the specified game.            | <id>      
help | Lists all available commands.           | [pattern]     
license | Displays licensing info.            |       
ma  | Adds a mod to the currently active game.        | <id> <file>    
md  | Deletes the specified mod and removes all associated files.    | <id>      
me  | Toggles if the selected mod is active.         | <id>      
ml  | Lists all mods for the currently active game.       | [pattern]     
mm  | Moves the specified mod to the specified position in the priority list. | <id> <position>   
top kek | Test command. Do not use, may cause death and/or destruction   |       
ucode | Toggles advanced unicode. (Enhanced characters)       | [on|true|yes|off|false|no] 
相关问题