Print and format a desk with printf
Generally it’s good to format the output of a console based Java program in a pleasant means.
The java.lang bundle has no built-in desk turbines, however you possibly can simply current knowledge in a chart-based format should you creatively use Java’s printf
technique with dashes, pipelines and printf
area specifiers.
A easy, Java printf desk displaying primitive sort particulars.
To reveal how one can format a desk with Java printf
statements, let’s create a chart that shows details about Java’s 8 primitive types.
Above is how the completed printf
desk will look once we are achieved. Let’s construct towards that because the purpose.
Java printf desk specs
We’ll format the Java printf
desk in keeping with the next guidelines:
- Dashed traces act as row separators.
- Pipelines separate columns.
- Every cell have a single house of padding on the left and proper.
- The primary column is 10 areas huge, plus padding.
- The second column is 8 areas huge, plus padding.
- The third column is 4 areas huge, plus padding.
- Integer values within the third column are zero padded to 4 digits.
Create the printf desk title
We will simply hardcode the title:
System.out.printf("--------------------------------%n"); System.out.printf(" Java's Primitive Varieties %n"); System.out.printf(" (printf desk instance) %n"); System.out.printf("--------------------------------%n");
The one printf
placeholder used within the first 4 traces is %n
, which merely causes a carriage return that places subsequent output on a brand new line.

This picture reveals the values used to create our desk with Java printf statements.
Code the printf desk’s heading
The center of the printf
desk will use the next three placeholders:
%-10s
to allocate 10 spaces for text String display. The minus indicators forces left justification.%-8s
to allocate 8 areas for textual content String show. Once more, the minus indicators forces left justification.%04d
, to allocate 4 areas to show the digits. The 0 in%04d
causes zero fills when a quantity will not be 4 digits lengthy.
With the added pipeline characters and padding, the Java printf
line to format the desk heading appears to be like as follows:
System.out.printf("| %-10s | %-8s | %4s |%n", "CATEGORY", "NAME", "BITS");
Java printf desk physique instance
The next is the entire code for the Java printf
desk instance’s physique:
System.out.printf("--------------------------------%n"); System.out.printf(" Java's Primitive Varieties %n"); System.out.printf(" (printf desk instance) %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", "lengthy", 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", "quick", 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 good-looking Java printf
desk shows, displaying a chart of the eight Java primitive varieties, together with attention-grabbing particulars about every of them.