我想组织这样的信息:
信息是用单元格组织的,而System.out.println
信息是非常混乱的。
您可以使用System.out.format()
or System.out.printf()
(在printf
内部简单地调用format
,因此两种方法都给出相同的结果)。
您将在下面找到将文本左对齐并用空格填充未使用位置的示例。左对齐字符串可以用 来实现%-15s
,这意味着:
%
保留(占位符)15
字符的“地方”s
字符串数据类型-
并从左侧开始打印。如果要处理数字,请使用d
后缀%-4d
,例如应放置在列左侧的最多 4 位数字。
顺便说一句printf
,打印数据后不会自动添加行分隔符,所以如果我们想将光标移动到下一行,我们需要自己做。我们可以使用\r
or \n
,或者让 Formatter 生成依赖于操作系统的行分隔符(如 Windows \r\n
)%n
(注意:这个“占位符”不需要任何数据作为参数,Java 将根据操作系统提供正确的序列)。
您可以在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 |
+-----------------+------+
试试这个替代方案:asciitable。
它提供了文本表的多种实现,最初使用 ASCII 和 UTF-8 字符作为边框。
这是一个示例表:
┌────────────────────────────────────────────────── ──────────────────────────┐ │ 表格标题 │ ├──────────────────┬──────────────────┬─────────── ────────┬──────────────────┤ │第一行(col1)│有一些│还有更多│甚至更多│ │ │ 信息 │ 信息 │ │ ├──────────────────┼──────────────────┼──────────── ────────┼──────────────────┤ │第二排│有一些│还有更多│甚至更多│ │ (col1) │ 信息 │ 信息 │ │ │ │ (col2) │ (col3) │ │ └──────────────────┴──────────────────┴──────────── ────────┴──────────────────┘</pre>查找最新版本: http: //mvnrepository.com/artifact/de.vandermeer/asciitable
我专门为此创建的课程是完全动态的: https ://github.com/2xsaiko/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]
采用System.out.printf()
例如,
String s = //Any string
System.out.printf(%10s, s);
将打印出 String 的内容,正好占用 10 个字符。因此,如果您想要一个表格,只需确保表格中的每个单元格都打印为相同的长度。另请注意,printf()
它不会打印新行,因此您必须自己打印。
您可以使用java-ascii-table。另见作者的网站。
这也很有效http://sourceforge.net/projects/texttablefmt/。Apache 也获得许可。
我这样做
示例:在 1 < x < 42 时打印具有 x² - x + 41 值的表格
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');
}
}
输出:
你可以用正确的方法使用 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
您可以使用 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);
我尝试使用这些功能解决此问题
这是您可以根据需要使用的简单纯 Java 解决方案。
输出看起来像这样。
+----+------------+--------------------------------+-----+--------------------------------+
| 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. |
| | | | | |
+----+------------+--------------------------------+-----+--------------------------------+
以防万一有人需要这种类型的表:
+----+----+----+----+----+
| 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("+----+----+----+----+----+");
}
}
}
}