0Pricing
Java Academy · Lesson

Aligning Tables

Format columns of data.

Aligning Tables is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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");
    }
}

Left Alignment with a Minus

Add a - flag to left-align inside the field. %-10s puts the text on the left and pads on the right.

Left alignment is ideal for labels and names.

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

Two Columns

Combine a left-aligned label with a right-aligned number to make a row. %-10s%5d gives a 10-wide name and a 5-wide number.

Repeat the same format for every row and the columns align.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%-10s%5d\n", "Apples", 3);
        System.out.printf("%-10s%5d\n", "Pears", 12);
    }
}

A Simple Table

Using the same format string for a header and each data row keeps everything aligned.

Here three rows share one pattern, so the columns stay tidy.

public class Main {
    public static void main(String[] args) {
        String fmt = "%-8s%6d\n";
        System.out.printf(fmt, "Name", 0);
        System.out.printf(fmt, "Ann", 90);
        System.out.printf(fmt, "Bob", 7);
    }
}

Aligning Decimals

For money or measurements, combine width and precision: %8.2f right-aligns a number in 8 characters with two decimals.

The decimal points line up vertically across rows.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%8.2f\n", 5.5);
        System.out.printf("%8.2f\n", 123.4);
    }
}

Header Plus Numbers

Mix string and number columns in one row. A left label and a right-aligned price read naturally together.

This pattern is the heart of most console reports.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%-10s%8.2f\n", "Coffee", 3.5);
        System.out.printf("%-10s%8.2f\n", "Cake", 12.0);
    }
}

Choosing Column Widths

Pick a width at least as large as your longest value, or text will overflow the column and break alignment.

If a value is wider than the field, printf prints it in full and ignores the width.

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

Looping Over Rows

A loop plus one shared format string makes generating a whole table easy.

Each pass prints one aligned row, so adding data never breaks the layout.

public class Main {
    public static void main(String[] args) {
        String[] names = {"Sun", "Mon", "Tue"};
        int[] hrs = {8, 12, 5};
        for (int i = 0; i < names.length; i++) {
            System.out.printf("%-6s%4d\n", names[i], hrs[i]);
        }
    }
}

Combining with String.format

You can build each aligned row with String.format if you need the text before printing.

The width and alignment rules are identical.

public class Main {
    public static void main(String[] args) {
        String row = String.format("%-8s%5d", "Total", 99);
        System.out.println(row);
    }
}

Tabs vs Widths

You could separate columns with \t tabs, but tab stops vary and long values misalign.

Fixed widths like %-10s give reliable, predictable columns, so prefer them for tables.

Quick Check

Which format left-aligns a name in 12 characters?

Recap

You built aligned tables using width fields, the - flag for left alignment, and %8.2f for tidy decimal columns.

With printf, String.format, and alignment, you can format any console output cleanly.

Frequently asked questions

Is the “Aligning Tables” lesson free?

Yes — the full text of “Aligning Tables” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Aligning Tables”?

Format columns of data. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Aligning Tables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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