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



Description

The Java Console readLine(String fmt, Object... args) method provides a formatted prompt, then reads a single line of text from the console.

Key Points

  • The readLine(String fmt, Object... args) method allows formatted prompts, making input collection more user-friendly.

  • It supports dynamic insertion of values into prompts using format specifiers like %s (string) and %d (integer).

  • Works only in console environments (not in most IDEs).

Declaration

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

public String readLine(String fmt, Object... args)

Parameters

  • fmt

  • args

Return Value

This method returns the string containing the line read from the console, not including any line-termination characters or null if an end of the stream has been reached.

Exception

  • IllegalFormatException − If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.

  • IOError − If an I/O error occurs.

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

The following example shows the usage of Java Console readLine(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;
      String username = "User";
      String alpha = null;
      
      try {
         // creates a console object
         console = System.console();

         // if console is not null
         if (console != null) {
            
            // read line from the user input
            alpha = console.readLine("%s! Enter Alphabets:", username);
            
            // prints
            System.out.println("Alphabets entered: " + alpha);
         }   
         
      } 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 −

User! Enter Alphabets:abc
Alphabets entered: abc

Example - Formatting a Prompt with User Input

The following example shows the usage of Java Console readLine(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 = System.console();
      if (console == null) {
         System.out.println("No console available");
         return;
      }

      // Using readLine(String fmt, Object... args) to format the prompt
      String city = console.readLine("What is your favorite city? ");

      // Display the result
      console.printf("Wow! %s is a great place to visit.\n", city);
   }
}

Console Interaction

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

What is your favorite city? Hyderabad
Wow! Hyderabad is a great place to visit.

Explanation

  • System.console() is used to get the Console instance.

  • We check if the console is available (this method doesn't work in most IDEs).

  • We use readLine(String fmt, Object... args) to print a formatted prompt and read user input.

  • The %s in printf() is replaced by the user's input to personalize the response.

Example - Asking Multiple Questions with Formatting

The following example shows the usage of Java Console readLine(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 = System.console();
      if (console == null) {
         System.out.println("No console available");
         return;
      }

      // Using readLine with formatted prompts
      String firstName = console.readLine("Enter your first name: ");
      String lastName = console.readLine("Enter your last name: ");
      int age = Integer.parseInt(console.readLine("Enter your age, %s %s: ", firstName, lastName));

      // Display user details
      console.printf("Hello, %s %s! You are %d years old.\n", firstName, lastName, age);
   }
}

Console Interaction

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

Enter your first name: Sunil
Enter your last name: Agarwal
Enter your age, Sunil Agarwal: 45
Hello, Sunil Agarwal! You are 45 years old.

Explanation

  • We first obtain the Console instance and check if it's available.

  • The readLine(String fmt, Object... args) method is used to format prompts dynamically, inserting the user's first and last name into the age question.

  • The age input is read and converted from String to int.

  • The user's full details are printed using printf().

java_io_console.htm
Advertisements