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.

java_io_console.htm
Advertisements