
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - Console printf(String fmt, Object... args) method
Description
The Java Console printf(String fmt, Object... args) method writes a formatted string to this console's output stream using the specified format string and arguments.
Declaration
Following is the declaration for java.io.Console.printf(String fmt, Object... args) method −
public Console printf(String fmt, Object... args)
Parameters
fmt − A format string as described in format string syntax
args − Arguments referenced by the format specifiers in the format string.
Return Value
This method returns this console.
Exception
IllegalprintfException − If a format string contains an illegal syntax, a format specifier that is incompatible with the given, insufficient arguments given the format string, or other illegal conditions
Important Note
System.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution.
Example - Usage of Console printf(String fmt, Object... args) method
The following example shows the usage of Java Console printf(String fmt, Object... args) method.
ConsoleDemo.java
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = null; try { console = System.console(); if (console != null) { String fmt = "%-10s %-10s %-10s%n"; // format console.printf(fmt, "Items", "Quanity", "Price"); console.printf(fmt, "-----", "-----", "-----"); console.printf(fmt, "Tomato", "1Kg", "15"); console.printf(fmt, "Potato", "5Kg", "50"); console.printf(fmt, "Onion", "2Kg", "30"); console.printf(fmt, "Apple", "4Kg", "80"); } } catch(Exception ex) { // if any error occurs ex.printStackTrace(); } } }
Console Interaction
Let us compile and run the above program, this will produce the following result −
Items Quantity Price ----- -------- ----- Tomato 1Kg 15 Potato 5Kg 50 Onion 2Kg 30 Apple 4Kg 80
Example - Printing Formatted User Details Using Console
The following example shows the usage of Java Console printf(String fmt, Object... args) method. This program prompts the user for their name and age and prints them in a formatted way using printf().
ConsoleDemo.java
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = System.console(); // Get Console object if (console == null) { System.out.println("No console available."); return; } String name = console.readLine("Enter your name: "); int age = Integer.parseInt(console.readLine("Enter your age: ")); console.printf("Hello, %s! You are %d years old.%n", name, age); } }
Console Interaction
Let us compile and run the above program, this will produce the following result −
Enter your name: Kumar Enter your age: 43 Hello, Kumar! You are 43 years old.
Explanation
System.console() is used to get the Console object.
readLine() is used to read the user's name and age.
-
printf() formats and prints the message−
%s− String placeholder (for name).
%d− Integer placeholder (for age).
%n− Newline character.
Example - Displaying a Formatted Table Using Console
The following example shows the usage of Java Console printf(String fmt, Object... args) method. This program displays tabular data using printf().
ConsoleDemo.java
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = System.console(); if (console == null) { System.out.println("No console available."); return; } console.printf("%-10s %-10s %-10s%n", "ID", "Name", "Score"); console.printf("%-10d %-10s %-10.2f%n", 101, "Alice", 85.5); console.printf("%-10d %-10s %-10.2f%n", 102, "Bob", 90.75); console.printf("%-10d %-10s %-10.2f%n", 103, "Charlie", 78.25); } }
Console Interaction
Let us compile and run the above program, this will produce the following result −
ID Name Score 101 Alice 85.50 102 Bob 90.75 103 Charlie 78.25
Explanation
The printf() method is used to format tabular data.
-
Format specifiers−
%-10s− Left-aligned string (width 10).
%-10d− Left-aligned integer (width 10).
%-10.2f− Left-aligned floating-point number (width 10, 2 decimal places).
This ensures structured tabular output.