我想组织这样的信息:
信息是按单元格组织的,而System.out.println
则信息非常混乱。
答案 0 :(得分:61)
您可以使用System.out.format()
或System.out.printf()
(printf
内部只调用format
,因此两种方法都会产生相同的结果。
下面您将找到将文本与左对齐并用空格填充未使用位置的示例。使用%-15s
可以将字符串左对齐,这意味着:
%
预留(占位符)15
“地方”字符s
字符串数据类型-
并从左侧开始打印。 如果您要处理数字,请使用d
后缀,例如%-4d
,以便将最多4位数字放在列的左侧。
BTW printf
在打印数据后不会自动添加行分隔符,因此如果想要将光标移动到下一行,我们需要自己完成。我们可以使用\r
或\n
,或者让Formatter生成依赖于操作系统的行分隔符(对于Windows \r\n
)我们可以使用%n
占位符(注意:它不会要求任何数据作为参数,Java将根据操作系统提供正确的序列。
您可以在documentation of that class找到Formatter
支持的语法的更多信息。
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 |
+-----------------+------+
答案 1 :(得分: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
答案 2 :(得分:7)
我专门为此而创建的课程是完全动态的: https://github.com/MRebhan/crogamp/blob/master/src/com/github/mrebhan/crogamp/cli/TableList.java
你可以像这样使用它:
data.mapValues(_ + 1).foreach(println)
(steve,6)
(bill,5)
(amzon,7)
(flikapr,8)
使用unicode字符会看起来像这样(注意:在控制台中看起来会更好,因为所有字符都相同):
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 chars(省略.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] ┃
┗━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
答案 3 :(得分:5)
使用System.out.printf()
例如,
String s = //Any string
System.out.printf(%10s, s);
将打印出String的内容,正好占用10个字符。因此,如果您想要一个表,只需确保表中的每个单元格打印出相同的长度。另请注意printf()
不会打印新行,因此您必须自己打印。
答案 4 :(得分:4)
您可以使用java-ascii-table。另请参阅the author's site。
答案 5 :(得分:1)
这也很有效http://sourceforge.net/projects/texttablefmt/。 Apache也获得许可。
答案 6 :(得分:1)
我是这样做的
示例:打印一个表格,其中 x² - x + 41 的值为 1 < x < 42
public class PrimeEquation
{
public static void main(String[] args)
{
String header = "";
header += formatDiv("a-----b-------------b----------c\n");
header += formatRow("| x | x² - x + 41 | Is Prime |\n");
header += formatDiv("d-----e-------------e----------f\n");
System.out.print(header);
for (int x = 2; x <= 41; x++)
{
int y = primeEquation(x);
String str1 = String.format("| %3d | %11d | %8b |", x, y, MyPrimes.isPrime(y));
System.out.println(formatRow(str1));
}
System.out.println(formatDiv("g-----h-------------h----------i"));
}
public static int primeEquation(int x)
{
return (x*x) - x + 41;
}
public static String formatRow(String str)
{
return str.replace('|', '\u2502');
}
public static String formatDiv(String str)
{
return str.replace('a', '\u250c')
.replace('b', '\u252c')
.replace('c', '\u2510')
.replace('d', '\u251c')
.replace('e', '\u253c')
.replace('f', '\u2524')
.replace('g', '\u2514')
.replace('h', '\u2534')
.replace('i', '\u2518')
.replace('-', '\u2500');
}
}
输出:
答案 7 :(得分: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
答案 8 :(得分:0)
答案 9 :(得分:0)
您可以使用Spring Shell实用程序类org.springframework.shell.table.TableModel:
TableModel model = new BeanListTableModel<>(portConfigurations, headers);
TableBuilder tableBuilder = new TableBuilder(model);
tableBuilder.addFullBorder(BorderStyle.oldschool);
//TableUtils.applyStyle(tableBuilder);
return tableBuilder.build().render(100);
答案 10 :(得分:0)
我尝试了使用这些功能的解决方案
这里是simple pure java solution,您可以根据需要使用它。
输出看起来像这样。
+----+------------+--------------------------------+-----+--------------------------------+
| id | First Name | Last Name | Age | Profile |
+----+------------+--------------------------------+-----+--------------------------------+
| 1 | John | Johnson | 45 | My name is John Johnson. My id |
| | | | | is 1. My age is 45. |
| | | | | |
| 2 | Tom | | 35 | My name is Tom. My id is 2. My |
| | | | | age is 35. |
| | | | | |
| 3 | Rose | Johnson Johnson Johnson Johnso | 22 | My name is Rose Johnson. My id |
| | | n Johnson Johnson Johnson John | | is 3. My age is 22. |
| | | son Johnson Johnson | | |
| | | | | |
| 4 | Jimmy | Kimmel | | My name is Jimmy Kimmel. My id |
| | | | | is 4. My age is not specified |
| | | | | . I am the host of the late ni |
| | | | | ght show. I am not fan of Matt |
| | | | | Damon. |
| | | | | |
+----+------------+--------------------------------+-----+--------------------------------+
答案 11 :(得分:0)
以防万一有人需要这种类型的表:
+----+----+----+----+----+
| 1 | 2 | 3 | 4 | 5 |
+----+----+----+----+----+
| 6 | 7 | 8 | 9 | 10 |
+----+----+----+----+----+
| 11 | 12 | 13 | 14 | 15 |
+----+----+----+----+----+
| 16 | 17 | 18 | 19 | 20 |
+----+----+----+----+----+
| 21 | 22 | 23 | 24 | 25 |
+----+----+----+----+----+
这是我的解决方案:
public class TableShape {
public static void main(String[] args) {
int width_and_height=5;
int count=1;
for(int i=0;i<width_and_height ; i++) {
System.out.println("+----+----+----+----+----+");
for(int j=0;j<width_and_height;j++) {
System.out.format("| %2d ", count++);
if(j==width_and_height-1) { // closing | for last column
System.out.print("|");
}
}
System.out.println();
if(i==width_and_height-1) { // closing line for last row
System.out.print("+----+----+----+----+----+");
}
}
}
}