Skip to content

Commit

Permalink
Merge pull request eugenp#17571 from anujgaud/ag/formatting-output-us…
Browse files Browse the repository at this point in the history
…ing-system-out

BAEL-6242 Add TableFormatter
  • Loading branch information
Maiklins authored Oct 12, 2024
2 parents a75559a + f5e99f1 commit d8b5538
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core-java-modules/core-java-io-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>de.vandermeer</groupId>
<artifactId>asciitable</artifactId>
<version>0.3.2</version>
<dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -81,4 +86,4 @@
<jmh.version>1.37</jmh.version>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
</properties>
</project>
</project>
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);
}
}

0 comments on commit d8b5538

Please sign in to comment.