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:
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
Parameter
Description
format
Format string with fixed text and specifiers. Must not be null.
args
Values for the specifiers, in order. May be empty when the format has no value specifiers.
Return Value
Return Value
Description
Console
The same console, so calls can be chained, e.g. console.printf("a%n").printf("b%n").
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]);
}
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsolePrintfMenu.
1) Basic 5 GB 0% off
2) Plus 50 GB 10% off
3) Pro 500 GB 25% off
How It Works
Menu numbers.i + 1 turns the zero-based loop index into the numbers 1, 2, 3 shown before each ).
Fixed-width name.%-6s pads "Basic", "Plus", and "Pro" to six characters, so the storage column starts at the same place on every line.
Right-aligned sizes.%4d gives 5, 50, and 500 four columns each, which keeps the "GB" labels lined up.
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);
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsolePrintfErrors. The last part is a run where System.console() is null (IDE, redirected input).
Missing value: MissingFormatArgumentException
Wrong type: IllegalFormatConversionException
5 apples
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
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.
Wrong kind of value."five" is a String, and %d accepts only whole-number types, so this call throws IllegalFormatConversionException.
One catch type for both. Both exceptions extend IllegalFormatException, and getSimpleName() prints the exact class without its package.
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);
}
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsolePrintfScores. Assumes an English (US) default locale, because format() and printf() use the default locale.
Name | Score
------|------
Ana | 90.0%
Bartho| 75.0%
Cy |100.0%
How It Works
Chained header. The first printf returns the console, so .printf(...) on the next line prints the dashed rule right after the column titles.
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.
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.
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.