回答
尝试使用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 |
+-----------------+------+
完美答案! ¡非常感谢! 我可以这样做,我想=) – 2013-03-08 16:20:27
对于固定的要求,这个解决方案是完美的。对于更真实的生活用例,图书馆更合适。看到这个答案:http://stackoverflow.com/a/35961774/363573。 – Stephan 2016-03-12 18:45:15
使用System.out.printf()
例如,
String s = //Any string
System.out.printf(%10s, s);
将打印出字符串s的内容,占用恰好10个字符。所以如果你想要一个表格,只要确保表格中的每个单元格都打印出相同的长度。另请注意,printf()
不会打印新行,因此您必须自己打印。
可以使用的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
你可以使用java-ascii-table。另见the author's site。
java-ascii-table似乎被放弃了。请参阅此答案的替代方法:http://stackoverflow.com/a/35961774/363573 – Stephan 2016-03-12 18:41:11
这也很好用http://sourceforge.net/projects/texttablefmt/。 Apache也许可。
texttablefmt似乎不可用在Maven上。这是Maven上提供的另一个库:http://stackoverflow.com/a/35961774/363573 – Stephan 2016-03-12 18:43:24
试试这个选择: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://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]
- 1. 如何使用asciitable.vim在gvim控制台中显示ascii表?
- 2. 如何在JavaScript中使用IRC创建控制台
- 3. 如何创建在线rebol控制台?
- 4. 创建控制台命令列表 - 如何正确使用Java
- 5. 如何在使用Python 3.x的控制台应用程序中创建ASCII动画?
- 6. 如何使用dbAcl使用控制台创建aro
- 7. Python在控制台/控制台中创建固定横幅
- 8. 如何从控制台创建snapshots.img?
- 9. 如何创建终端/控制台
- 10. 如何使用滑轨控制台从表格中删除列
- 11. 如何在Heroku控制台中创建用户
- 12. 如何创建一个风格控制台输出,如Facebook
- 13. 如何在linux中使用控制台使用java keytool创建发行证书?
- 14. 在yii2控制台中创建url
- 15. 在python中创建控制台
- 16. 如何在php中使用控制台?
- 17. 如何在android中使用控制台
- 18. Firebase控制台:如何删除表格?
- 19. 使用netbeans在Java中创建控制台应用程序?
- 20. 在控制台应用程序中使用创建的DLL?
- 21. 如何创建格式化的javascript控制台日志消息
- 22. 从非控制台.NET应用程序内创建控制台
- 23. 如何使用控制台
- 24. 使用shopify_token在控制台中手动创建Shopify会话
- 25. 使用数据库中的数据在控制台中创建格式良好的表格
- 26. 在componentsSeparatedByString中使用ASCII控制代码
- 27. 如何在Yii2中创建控制台命令(rake任务)?
- 28. 如何在控制台中创建记录 - ROLLBACK TO SAVEPOINT active_record_1?
- 29. Java。如何在IDEA控制台中创建进度条?
- 30. 如何在控制台中创建与Java的键绑定?
+1。不是一个坏问题。如果有人有这个好的图书馆,请推荐。否则,看看System.out.format – Thilo 2013-03-05 03:32:45
你可以查看[this](http://stackoverflow.com/questions/15193812/how-to-print-a-table-of-arrays/15194265#15194265)示例 – MadProgrammer 2013-03-05 03:33:56
您最好的投注主要是使用'System.out.printf(...)'和等效的'System.out.format(...)'。 – 2013-03-05 03:34:46