Java - Console readLine() method



Description

The Java Console readLine() method reads a single line of text from the console.

Declaration

Following is the declaration for java.io.Console.readLine() method −

public String readLine()

Parameters

NA

Return Value

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

Exception

IOException− 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() method

The following example shows the usage of Java Console readLine() method.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console console = null;
      String name = null;
      
      try {
         // creates a console object
         console = System.console();

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

Name: Master Programmer
Name entered : Master Programmer

Example - Reading a Password Securely

The following example shows the usage of Java Console readLine() 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;
      }

      // Reading username using readLine()
      String username = console.readLine("Enter your username: ");

      // Reading password securely using readPassword()
      char[] passwordChars = console.readPassword("Enter your password: ");
      String password = new String(passwordChars); // Convert char array to string

      // Display output
      System.out.println("Username: " + username);
      System.out.println("Password length: " + password.length()); // Avoid printing actual password
   }
}

Console Interaction

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

Enter your username: akumar
Enter your password:

Username: akumar
Password length: 8

Explanation

  • We get the Console instance using System.console().

  • If the console is unavailable (e.g., when running in an IDE), we print a message and exit.

  • We use readLine() to read a username as plain text.

  • We use readPassword() to read a password securely (it does not display input characters on the screen).

  • Since readPassword() returns a char[], we convert it to a String.

  • Instead of printing the password directly (which is a security risk), we print its length.

This method ensures secure handling of passwords in a console application.

Example - Reading Multiple Inputs

The following example shows the usage of Java Console readLine() 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;
      }

      // Read multiple inputs
      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: "));

      // Display the collected inputs
      System.out.println("Full Name: " + firstName + " " + lastName);
      System.out.println("Age: " + age);
   }
}

Console Interaction

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

Enter your first name: Anil
Enter your last name: Kumar
Enter your age: 43
Full Name: Anil Kumar
Age: 43

Explanation

  • Similar to the first example, we obtain the Console instance and check if it's available.

  • We use readLine() multiple times to read different pieces of user input.

  • The Integer.parseInt() method is used to convert the age input from a String to an int.

  • Finally, we display the collected inputs.

java_io_console.htm
Advertisements