Java - Console format(String fmt, Object... args) method



Description

The Java Console format(String fmt, Object... args) method writes a formatted string to this console's output stream using the specified format string arguments.

Declaration

Following is the declaration for java.io.Console.format(String fmt, Object... args) method −

public Console format(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

IllegalFormatException − 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 format(String fmt, Object... args) method

The following example shows the usage of Java Console format(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.format(fmt, "Items", "Quanity", "Price");
            console.format(fmt, "-----", "-----", "-----");
            console.format(fmt, "Tomato", "1Kg", "15");
            console.format(fmt, "Potato", "5Kg", "50");
            console.format(fmt, "Onion", "2Kg", "30");
            console.format(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 - Formatting and Displaying User Input

The following example shows the usage of Java Console format(String fmt, Object... args) method. This program prompts the user for their name and age, then formats and displays the input using format().

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.format("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: Anil
Enter your age: 32
Hello, Anil! You are 32 years old.

Explanation

  • The System.console() method is used to get a Console object.

  • It checks if the console is available (to prevent errors in environments that don't support Console).

  • The readLine() method is used to get the user's name and age as input.

  • The format() method formats and displays the user's input in a structured way using format specifiers (%s for string, %d for integer).

Example - Displaying a Table Using format()

The following example shows the usage of Java Console format(String fmt, Object... args) method. This program demonstrates how to format tabular data.

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.format("%-10s %-10s %-10s%n", "ID", "Name", "Marks");
      console.format("%-10s %-10s %-10d%n", "101", "Alice", 85);
      console.format("%-10s %-10s %-10d%n", "102", "Bob", 90);
      console.format("%-10s %-10s %-10d%n", "103", "Charlie", 78);
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

ID         Name       Marks
101        Alice      85
102        Bob        90
103        Charlie    78

Explanation

  • The format() method is used to display tabular data with left-aligned columns.

  • The format string−

    • %-10s − Left-aligned string of 10 characters.

    • %-10d − Left-aligned integer of 10 characters.

  • This helps in neatly displaying data in a tabular format.

java_io_console.htm
Advertisements