1. Overview
The Java standard library provides the printf() and format() methods to output formatted data to the console. These two methods make it possible to create a table using ASCII characters in a console app. Also, there’s a third-party library named AsciiTable that further simplifies the task.
In this tutorial, we’ll learn how to use the Java standard API and a third-party API to create a table using ASCII characters in Java.
2. Project Setup
To understand how to output a table to the console in Java, let’s create a simple project that outputs a person’s name, height, weight, and body mass index (BMI) to the console.
First, let’s create a class named BodyMassIndex:
class BodyMassIndex {
private String name;
private double height;
private double weight;
// constructor, getters and setters
double calculate() {
double bmi = weight / (height * height);
String formattedBmi = String.format("%.2f", bmi);
return Double.parseDouble(formattedBmi);
}
}
Here, we create a class named BodyMassIndex. Its constructor accepts name, height, and weight as parameters. Also, we define a method named calculate() to compute body mass index.
We’ll also create a new class named BodyMassIndexApplication, which will have methods that use the BodyMassIndex object to construct a table using ASCII characters.
Next, let’s create BodyMassIndex objects in the class and store them in an ArrayList:
List<BodyMassIndex> bodyMassIndices = new ArrayList<>();
bodyMassIndices.add(new BodyMassIndex("Tom", 1.8, 80));
bodyMassIndices.add(new BodyMassIndex("Elton", 1.9, 90));
bodyMassIndices.add(new BodyMassIndex("Harry", 1.9, 90));
bodyMassIndices.add(new BodyMassIndex("Hannah", 1.9, 90));
In the subsequent sections, we’ll output the data in a tabular form to the console using System.out.format() and AsciiTable.
3. Using the System.out.format() method
The Java PrintStream object System.out provides methods like format() and print() to output format string to the console. Both are handy for constructing a table using ASCII characters. We can use these methods to carefully place ASCII characters to draw lines and position data.
3.1. System.out.format()
We’ll use format specifiers to place the data in the right column correctly. Let’s see an example code that outputs a table to the console using ASCII characters:
System.out.format("+---------+---------+---------+-------+%n");
System.out.format("| Name | Height | Weight | BMI |%n");
System.out.format("+---------+---------+---------+-------+%n");
String leftAlignment = "| %-7s | %-7.2f | %-7.2f | %-5.2f |%n";
for (BodyMassIndex bodyMassIndex : bodyMassIndices) {
System.out.format(leftAlignment, bodyMassIndex.getName(), bodyMassIndex.getHeight(), bodyMassIndex.getWeight(), bodyMassIndex.calculate());
System.out.format("+---------+---------+---------+-------+%n");
}
In the code above, we create a header of four columns for the table. First, we use the plus sign to show the beginning and ending of each column. Next, we use hyphens to draw horizontal lines. Then, we use the newline character to terminate each line.
Furthermore, we use the pipe sign to draw a vertical line. The characters +, –, and | signs are arranged based on the structure of the table.
Finally, we declare a string variable named leftAlignment and assign it with values of format String. The format string helps format output to the console. The format string contains the following elements:
- | – Separate the columns in the output
- %-7s – Helps to left-align a string and use a minimum field width of 7 characters
- %-7.2f – Helps to left-align a float and to use a minimum field width of 7 characters and 2 decimal places
- %-5.2f – Helps set the minimum field width to 5 and 2 decimal places
- %n – Newline character
Alternatively, we can use System.out.printf() in place of System.out.format(). Both methods provide the same result.
3.2. The Output
Here’s the generated table:
The console displays a table constructed using ASCII characters. The table is rendered on the console based on our specifications.
4. Using the AsciiTable Library
AsciiTable is a third-party library that makes it easy to create a nice-looking ASCII table.
4.1. The AsciiTable Library
To use the AsciiTable library, let’s add its dependency to the pom.xml:
<dependency>
<groupId>de.vandermeer</groupId>
<artifactId>asciitable</artifactId>
<version>0.3.2</version>
</dependency>
Next, let’s see an example code that uses the library to create the BMI data table in ASCII format:
AsciiTable asciiTable = new AsciiTable();
asciiTable.addRule();
asciiTable.addRow("Name", "Height", "Weight", "BMI");
asciiTable.addRule();
for (BodyMassIndex bodyMassIndex : bodyMassIndices) {
asciiTable.addRow(bodyMassIndex.getName(), bodyMassIndex.getHeight(), bodyMassIndex.getWeight(), bodyMassIndex.calculate());
asciiTable.addRule();
}
asciiTable.setTextAlignment(TextAlignment.CENTER);
String render = asciiTable.render();
System.out.println(render);
In the code above, we create an AsciiTable object. Next, we invoke @addRule() on it to add a horizontal line. Then, we use the addRow() method to populate the AsciiTable table object with data.
Also, the AsciiTable class provides methods to format the data. We align the data to the center by invoking the setTextAlignment() on the AsciiTable object. The method accepts enum as an argument to specify the text alignment.
Finally, we invoke the render(), which returns a string on the AsciiTable object.
4.2. The Output
Here’s the output on the console:
The AsciiTable library provides an easy way to create nice-looking ASCII tables for the console with minimal code.
5. Conclusion
In this article, we learned how to output a table to the console using the built-in System.out.format() method and the AsciiTable library.
Both methods provide a working way to achieve the task. However, while using the AsciiTable library requires less work to align columns properly, the System.out.format() method gives more direct control over styling.
As usual, the complete source code for the examples is available over on GitHub.