Java Console format() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

In Java, console.format(fmt, args...) builds text from a format string and a list of values, writes it to the terminal, and flushes it. Specifiers such as %s (text), %d (whole number), %.2f (decimal with two places), and %n (line break) mark where each value goes.

It follows the same rules as String.format(), but prints the result instead of returning it. It returns the Console itself, so several calls can be chained. printf() is an exact alias.

An instance method of the java.io.Console class, available since Java 6. You never create a Console yourself: get it with Console console = System.console(); after import java.io.Console;, and check it for null before calling console.format(...).

Locale assumption.

Console formatting uses the default locale (Locale.getDefault(Locale.Category.FORMAT)). The outputs on this page assume an English (US) default locale. In a German or French locale, %.2f prints a comma as the decimal separator, for example 7,00.

Syntax

Pass a format string, then one value for each specifier:

Java
Console console = System.console();
console.format("Hello, %s!%n", "Ana");             // Hello, Ana!
console.format("%d items cost %.2f%n", 3, 7.5);    // 3 items cost 7.50
console.format("[%5s] [%-5s]%n", "ab", "cd");      // [   ab] [cd   ]
CallWhat it does
console.format("%s", text)Inserts the text as it is.
console.format("%5d", n)Right-aligns a whole number in 5 columns.
console.format("%-8s", text)Left-aligns text in 8 columns.
console.format("%,.2f", x)Two decimals with grouping, e.g. 1,234.50 in an English locale.
console.format("%n")Prints the platform line separator.

Available since Java 6. Recent JDKs (Java 23 and later) also offer an overload that takes a Locale first. On older versions, when you need a fixed locale, build the text with String.format(Locale.US, ...) and print it with %s.

Library declarations
Java
// java.io.Console
public Console format(String fmt, Object... args)
public Console printf(String format, Object... args)   // same behaviour

Parameters

ParameterDescription
fmtFormat string: fixed text plus specifiers such as %s, %d, %.2f, and %n. Must not be null.
argsValues for the specifiers, in order. Extra values are ignored; missing ones cause an exception.

Return Value

Return ValueDescription
ConsoleThe same console object, so you can chain: console.format(...).format(...).

Compare with Similar Methods

Other ways to print formatted text:

MethodHow it differs
printf()Identical behaviour under a different name.
String.format()Returns the formatted String instead of printing it.
System.out.printf()Formats to System.out, which exists even without a console.
writer().printf()Same formatting through the console PrintWriter.

Java Examples

Example 1 — Print a Neatly Aligned Receipt

Use widths and the - flag to line up item names, quantities, and amounts in columns.

Java
import java.io.Console;

public class ConsoleFormatReceipt {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.out.println("No console available. Run this program from a terminal.");
            return;
        }
        String[] items = {"Coffee", "Bagel", "Juice"};
        int[] qty = {2, 1, 3};
        double[] price = {3.5, 2.25, 4.0};
        double total = 0;
        console.format("%-8s %3s %8s%n", "Item", "Qty", "Amount");
        for (int i = 0; i < items.length; i++) {
            double amount = qty[i] * price[i];
            total += amount;
            console.format("%-8s %3d %8.2f%n", items[i], qty[i], amount);
        }
        console.format("%-8s %3s %8.2f%n", "Total", "", total);
    }
}

How It Works

  1. Header with the same widths. The heading uses %-8s %3s %8s, matching the row pattern, so "Item", "Qty", and "Amount" sit exactly above their columns.
  2. Left versus right. %-8s pads names on the right (text reads left to right), while %3d and %8.2f pad numbers on the left so their last digits line up.
  3. Rounded money. 2 x 3.5 = 7.0, 1 x 2.25 = 2.25 and 3 x 4.0 = 12.0; %8.2f always shows two decimals, giving 7.00, 2.25, and 12.00.
  4. Empty middle cell. The total row passes "" to %3s, which prints three spaces, so 21.25 still lands in the amount column.

Example 2 — Chain format() Calls Using the Returned Console

format() returns the console it was called on, so you can call it again on the result.

Java
import java.io.Console;

public class ConsoleFormatChain {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.out.println("No console available. Run this program from a terminal.");
            return;
        }
        Console same = console.format("Step %d of %d: %s%n", 1, 3, "download")
                              .format("Step %d of %d: %s%n", 2, 3, "unpack");
        console.format("Step %d of %d: %s%n", 3, 3, "install");
        console.format("Same object returned? %b%n", same == console);
    }
}

How It Works

  1. Null guard first. In a terminal the guard is skipped; without a console, the program prints the fallback line and returns before any format() call could throw NullPointerException.
  2. Call on the result. The first format() prints step 1 and returns the console, and the second .format(...) runs on that returned object to print step 2.
  3. Three values per line. Each call fills %d, %d, and %s from left to right with the step number, the total, and the task name.
  4. Identity check. %b prints the boolean same == console, which is true: chaining never creates a new object.

Example 3 — Default Locale vs a Fixed Locale

Console.format() always uses the default locale. To force a style, format the text with String.format(Locale, ...) first.

Java
import java.io.Console;
import java.util.Locale;

public class ConsoleFormatLocale {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) {
            System.out.println("No console available. Run this program from a terminal.");
            return;
        }
        double distance = 1234.5;
        console.format("Default locale: %,.2f km%n", distance);
        console.format("%s%n", String.format(Locale.GERMANY, "German style:   %,.2f km", distance));
        console.format("%s%n", String.format(Locale.US, "US style:       %,.2f km", distance));
    }
}

How It Works

  1. Grouping flag. The , in %,.2f asks for a thousands separator, and .2 keeps two decimals, so 1234.5 becomes 1,234.50 with the assumed English default locale.
  2. German rules. String.format(Locale.GERMANY, ...) swaps the symbols: a dot groups thousands and a comma marks decimals, giving 1.234,50.
  3. Print the finished text. The already-formatted string is passed to console.format("%s%n", ...), so the console does no number formatting of its own and the chosen locale survives.
  4. Predictable output. The Locale.US line looks the same on every computer, which is why fixed locales are used for logs and files.

Frequently Asked Questions

It formats its arguments according to a format string, writes the result to the console, and flushes the output.
There is none. printf is documented as a convenience method that behaves exactly like format with the same arguments.
The same Console object it was called on, which lets you chain calls such as console.format("a%n").format("b%n").
The default format locale of the JVM. To force a locale on Java 8, build the text with String.format(Locale.US, ...) and print it with %s.
A missing value or a wrong type throws an IllegalFormatException subclass, such as MissingFormatArgumentException or IllegalFormatConversionException. Extra values are ignored.

Next: printf()

Print formatted text to the terminal; a convenience alias of format().

printf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful