Java Console printf() Method

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

Definition

In Java, console.printf(format, args...) writes formatted text to the terminal. It is a convenience method: a call to printf behaves exactly like the same call to format(). The name will feel familiar if you know C.

Each % specifier can carry a width (minimum number of columns), the - flag (left-align), and a precision (decimal places for numbers, maximum characters for text). Write %% for a literal percent sign.

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.printf(...).

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

A specifier has the shape %[flags][width][.precision]conversion:

Java
console.printf("%d%n", 42);               // 42
console.printf("%6.2f%n", 3.14159);       //   3.14
console.printf("%-6s|%n", "Ana");         // Ana   |
console.printf("%.3s%n", "Bartholomew");  // Bar
console.printf("%d%%%n", 75);             // 75%
CallWhat it does
console.printf("%d", n)Whole number (byte, short, int, long, BigInteger).
console.printf("%6.2f", x)Decimal number, 2 places, at least 6 columns wide.
console.printf("%-6s", s)Text left-aligned in 6 columns.
console.printf("%.3s", s)At most the first 3 characters of the text.
console.printf("%b %c %x", ...)A boolean, a single character, and a number in hexadecimal.

Available since Java 6. Like format(), every printf() call flushes and returns the Console. Recent JDKs (Java 23 and later) add an overload with a Locale parameter.

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

Parameters

ParameterDescription
formatFormat string with fixed text and specifiers. Must not be null.
argsValues for the specifiers, in order. May be empty when the format has no value specifiers.

Return Value

Return ValueDescription
ConsoleThe same console, so calls can be chained, e.g. console.printf("a%n").printf("b%n").

Compare with Similar Methods

printf() and its relatives:

MethodHow it differs
format()The method printf forwards to; identical results.
System.out.printf()PrintStream version that works without a console.
String.format()Returns the text instead of printing it.
readLine(fmt, args)Prints a formatted prompt, then reads a line.

Java Examples

Example 1 — Print a Numbered Plan Menu with a Percent Sign

Combine %d, %-6s, %4d, and %% to print an aligned menu.

Java
import java.io.Console;

public class ConsolePrintfMenu {
    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[] plans = {"Basic", "Plus", "Pro"};
        int[] gigabytes = {5, 50, 500};
        int[] discount = {0, 10, 25};
        for (int i = 0; i < plans.length; i++) {
            console.printf("%d) %-6s %4d GB  %2d%% off%n", i + 1, plans[i], gigabytes[i], discount[i]);
        }
    }
}

How It Works

  1. Menu numbers. i + 1 turns the zero-based loop index into the numbers 1, 2, 3 shown before each ).
  2. Fixed-width name. %-6s pads "Basic", "Plus", and "Pro" to six characters, so the storage column starts at the same place on every line.
  3. Right-aligned sizes. %4d gives 5, 50, and 500 four columns each, which keeps the "GB" labels lined up.
  4. Literal percent. In %2d%%, the first part prints the discount in two columns and %% prints one % character.

Example 2 — Catch IllegalFormatException from Bad printf Calls

Forgetting a value or passing the wrong type compiles fine but fails at run time. Catch the common parent, IllegalFormatException.

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

public class ConsolePrintfErrors {
    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;
        }
        try {
            console.printf("%d apples%n");
        } catch (IllegalFormatException e) {
            console.printf("Missing value: %s%n", e.getClass().getSimpleName());
        }
        try {
            console.printf("%d apples%n", "five");
        } catch (IllegalFormatException e) {
            console.printf("Wrong type: %s%n", e.getClass().getSimpleName());
        }
        console.printf("%d apples%n", 5);
    }
}

How It Works

  1. No value at all. printf("%d apples%n") has a %d but an empty argument list, so the formatter throws MissingFormatArgumentException before printing anything, because %d comes first.
  2. Wrong kind of value. "five" is a String, and %d accepts only whole-number types, so this call throws IllegalFormatConversionException.
  3. One catch type for both. Both exceptions extend IllegalFormatException, and getSimpleName() prints the exact class without its package.
  4. The correct call. Passing the int 5 matches %d, so the last line prints normally.

Example 3 — Truncate Long Names and Print Percentages

A precision on %s cuts long text, and a precision on %f rounds numbers. The two header lines come from one chained call.

Java
import java.io.Console;

public class ConsolePrintfScores {
    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[] names = {"Ana", "Bartholomew", "Cy"};
        int[] correct = {18, 15, 20};
        int questions = 20;
        console.printf("%-6s|%6s%n", "Name", "Score")
               .printf("%-6s|%6s%n", "------", "------");
        for (int i = 0; i < names.length; i++) {
            double percent = correct[i] * 100.0 / questions;
            console.printf("%-6.6s|%5.1f%%%n", names[i], percent);
        }
    }
}

How It Works

  1. Chained header. The first printf returns the console, so .printf(...) on the next line prints the dashed rule right after the column titles.
  2. Width plus precision on text. %-6.6s pads short names to six characters and cuts longer ones to six, so "Bartholomew" becomes "Bartho" and the bar stays aligned.
  3. Percent math. Multiplying by 100.0 forces decimal arithmetic: 18/20 is 90.0, 15/20 is 75.0, and 20/20 is 100.0.
  4. Number then sign. %5.1f prints one decimal in five columns (100.0 fills all five), and %% adds the percent sign.

Frequently Asked Questions

Yes. printf is defined to behave exactly like format with the same arguments, and it also returns the Console.
Write %% in the format string. For example console.printf("%d%%%n", 75) prints 75%.
Left-align the text (the - flag) in at least 6 columns (width 6) and print at most 6 characters (precision .6), so longer text is cut off.
%d needs a whole-number type and "5" is a String, so Java throws IllegalFormatConversionException at run time. Pass the number 5, or convert with Integer.parseInt("5").
Use %n. It prints the line separator of the current platform, while \n is always a single line feed character.

Next: readLine()

Read one line of text typed by the user, optionally after a formatted prompt.

readLine() 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.

10 people found this page helpful