How to format a Java table with printf example

Print and format a table with printf

Sometimes it’s nice to format the output of a console based Java program in a friendly way.

There are no built-in table generators in the java.lang package, but you can easily present data in a chart based format if you creatively use Java’s printf method with dashes, pipelines and printf field specifiers.

java printf table

A simple, Java printf table showing primitive type details.

To demonstrate how to format a table with Java printf statements, let’s create a chart that displays information about Java’s 8 primitive types.

Here’s how the finished printf table will look when we are done. Let’s build towards this as the goal.

Java printf table specifications

The Java printf table will be formatted according to the following rules:

  • Dashed lines will act as row separators
  • Pipelines will separate columns
  • Each cell will have a single space of padding on the left and right
  • The first column will be 10 spaces wide, plus padding
  • The second column will be 8 spaces wide, plus padding
  • The third column will be 4 spaces wide, plus padding
  • Integer values in the 3rd column will be zero padded to four digits

Create the printf table title

We can easily hardcode the title:

System.out.printf("--------------------------------%n");
System.out.printf("    Java's Primitive Types      %n");
System.out.printf("    (printf table example)      %n");
System.out.printf("--------------------------------%n");

The only printf placeholder used in the first four lines is %n, which simply causes a carriage return so subsequent output happens on a new line.

Java Printf Table chart

This image shows the values used to create our table with Java printf statements.

Code the printf table’s heading

The guts of the printf table will use the following three placeholders:

  • %-10s to allocate 10 spaces for text String display. The minus signs forces left justification.
  • %-8s to allocate 8 spaces for text String display. Again, the minus signs forces left justification.
  • %04d, to allocate 4 spaces to display the digits. The 0 in %04d causes zero fills when a number is not 4 digits long

When the pipeline characters and padding is added, the Java printf line to format the table heading looks as follows:

System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS");

Java printf table body example

The following is the complete code for the Java printf table example’s body:

System.out.printf("--------------------------------%n");
System.out.printf(" Java's Primitive Types         %n");
System.out.printf(" (printf table example)         %n");

System.out.printf("--------------------------------%n");
System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS");
System.out.printf("--------------------------------%n");

System.out.printf("| %-10s | %-8s | %04d |%n", "Floating", "double",  64);
System.out.printf("| %-10s | %-8s | %04d |%n", "Floating", "float",   32);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "long",    64);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "int",     32);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "char",    16);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "short",   16);
System.out.printf("| %-10s | %-8s | %04d |%n", "Integral", "byte",    8);
System.out.printf("| %-10s | %-8s | %04d |%n", "Boolean",  "boolean", 1);

System.out.printf("--------------------------------%n");

When the code runs, a handsome Java printf table displays, showing a chart of the 8 Java primitive types, along with interesting details about each of them.

Format Output with Java printf Tutorials

Here are some great tutorials and examples on how to use printf to format Java output.

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close