以下のように情報を整理したいと思います。
情報はセルで整理されてSystem.out.println
いますが、情報は非常にまとまりがありません。
System.out.format()
orを使用できますSystem.out.printf()
(printf
内部的に単に呼び出すformat
ため、両方のメソッドが同じ結果を返します)。
以下に、テキストを左揃えにし、未使用の場所をスペースで埋める例を示します。文字列を左に揃えるには%-15s
、 を使用します。つまり、次のことを意味します。
%
予約 (プレースホルダー)15
文字の「場所」s
String データ型の-
左から印刷を開始します。数字を処理したい場合は、列の左側に配置する必要がある最大 4 桁の数字のd
ような接尾辞を使用します。%-4d
ところでprintf
、印刷されたデータの後に自動的に行区切りを追加しないため、カーソルを次の行に移動したい場合は、自分で行う必要があります。orを使用する\r
か\n
、Formatter に OS 依存の行区切り文字 (Windows の場合など\r\n
) を生成させることができます (注: この「プレースホルダー」は引数として%n
データを必要としません。Java は OS に基づいて正しいシーケンスを提供します)。
サポートされている構文の詳細については、 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 |
+-----------------+------+
この代替手段を試してください: ascitable。
テキスト テーブルのいくつかの実装を提供します。もともとは境界線に ASCII および UTF-8 文字を使用していました。
サンプルテーブルは次のとおりです。
┌────────────────────────────────────────────────── ──────────────────────────┐ │ 表の見出し │ ├──────────────────┬──────────────────┬─────────── ────────┬──────────────────┤ │ 最初の行 (col1) │ いくつかの │ その他 │ さらに │ │ │ インフォメーション │ インフォメーション │ │ ├──────────────────┼──────────────────┼─────────── ────────┼──────────────────┤ │ 2 行目 │ いくつか │ もっと │ さらに │ │ (col1) │ 情報 │ 情報 │ │ │ │ (列2) │ (列3) │ │ └──────────────────┴──────────────────┴─────────── ────────┴──────────────────┘</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もライセンスされています。
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もご覧ください
誰かがこのタイプのテーブルを必要とする場合に備えて:
+----+----+----+----+----+
| 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("+----+----+----+----+----+");
}
}
}
}