0Pricing
Java Academy · Lesson

Aligning Tables

Format columns of data.

Why Alignment?

When you print rows of data, ragged columns are hard to read. Aligned columns line numbers and labels neatly under each other.

printf widths and flags let you build clean tables right in the console.

Right Alignment by Default

A width like %10s reserves 10 characters and pads on the left, so text is right-aligned.

Numbers behave the same way, which is why columns of figures line up on the right edge.

public class Main {
    public static void main(String[] args) {
        System.out.printf("[%10s]\n", "hi");
    }
}

All lessons in this course

  1. printf Basics
  2. Number and Decimal Formats
  3. String.format
  4. Aligning Tables
← Back to Java Academy