forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#17571 from anujgaud/ag/formatting-output-us…
…ing-system-out BAEL-6242 Add TableFormatter
- Loading branch information
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...java-modules/core-java-io-5/src/main/java/com/baeldung/tableformatter/TableFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.baeldung.tableformatter; | ||
|
||
import de.vandermeer.asciitable.AsciiTable; | ||
|
||
public class TableFormatter { | ||
|
||
static void usingStringConcatenation() { | ||
String[] headers = {"ID", "Name", "Age"}; | ||
String[][] data = { | ||
{"1", "James", "24"}, | ||
{"2", "Sarah", "27"}, | ||
{"3", "Keith", "31"} | ||
}; | ||
|
||
System.out.println(headers[0] + " " + headers[1] + " " + headers[2]); | ||
|
||
for (String[] row : data) { | ||
System.out.println(row[0] + " " + row[1] + " " + row[2]); | ||
} | ||
} | ||
|
||
static void usingPrintf() { | ||
String[] headers = {"ID", "Name", "Age"}; | ||
String[][] data = { | ||
{"1", "James", "24"}, | ||
{"2", "Sarah", "27"}, | ||
{"3", "Keith", "31"} | ||
}; | ||
|
||
System.out.printf("%-5s %-10s %-5s%n", headers[0], headers[1], headers[2]); | ||
|
||
for (String[] row : data) { | ||
System.out.printf("%-5s %-10s %-5s%n", row[0], row[1], row[2]); | ||
} | ||
} | ||
|
||
static void usingStringFormat() { | ||
String[] headers = {"ID", "Name", "Age"}; | ||
String[][] data = { | ||
{"1", "James", "24"}, | ||
{"2", "Sarah", "27"}, | ||
{"3", "Keith", "31"} | ||
}; | ||
|
||
String header = String.format("%-5s %-10s %-5s", headers[0], headers[1], headers[2]); | ||
System.out.println(header); | ||
|
||
for (String[] row : data) { | ||
String formattedRow = String.format("%-5s %-10s %-5s", row[0], row[1], row[2]); | ||
System.out.println(formattedRow); | ||
} | ||
} | ||
|
||
static void usingStringBuilder() { | ||
String[] headers = {"ID", "Name", "Age"}; | ||
String[][] data = { | ||
{"1", "James", "24"}, | ||
{"2", "Sarah", "27"}, | ||
{"3", "Keith", "31"} | ||
}; | ||
|
||
StringBuilder table = new StringBuilder(); | ||
|
||
table.append(String.format("%-5s %-10s %-5s%n", headers[0], headers[1], headers[2])); | ||
|
||
|
||
for (String[] row : data) { | ||
table.append(String.format("%-5s %-10s %-5s%n", row[0], row[1], row[2])); | ||
} | ||
|
||
System.out.print(table.toString()); | ||
} | ||
|
||
static void usingAsciiTable() { | ||
AsciiTable table = new AsciiTable(); | ||
table.addRule(); | ||
table.addRow("ID", "Name", "Age"); | ||
table.addRule(); | ||
table.addRow("1", "James", "24"); | ||
table.addRow("2", "Sarah", "27"); | ||
table.addRow("3", "Keith", "31"); | ||
table.addRule(); | ||
|
||
String renderedTable = table.render(); | ||
System.out.println(renderedTable); | ||
} | ||
} |