Console readPassword() in Java with examples

In this tutorial, we will see the use of readPassword() method in Java along with examples.
The readPassword() method belongs to the Console class in Java. It allows the user to type passwords or passphrases with echoing disabled.

Here we will discuss the two common ways to use the readPassword() method.

Java Console.readPassword()

Syntax

public char[] readPassword()

This method does not accept any parameters. It returns a character array that stores the password from the console. It returns null at the end of the stream. The method also shows an IOException in case of an I/O error.

Let us see an example program using this method,

import java.io.Console;

public class Password {
  public static void main(String[] args) {

    Console c = System.console();

    if (c == null) 
    {  
      System.out.println("Console Not Available!");
    }    
    else
    {
      System.out.println("Enter User Name: "); 
      //Reads user name
      String user = c.readLine(); 
      
      System.out.println("Enter Password: ");
      //Reads password by masking
      char[] pass = c.readPassword();
      
      //Converts char array to String
      String password = String.valueOf(pass);
      
      //Displays user name
      System.out.println("The User Name is: " + user);
      //Displays Password
      System.out.println("The Password is: " + password);
    }
  }
}

Output

Enter User Name:
Admin
Enter Password:

The User Name is: Admin
The Password is: 12345

Note:

If the password read from the console is not converted into String then it does not give the desired output.

Console.readPassword(String fmt, Object args)

Syntax

public char[] readPassword(String fmt, Object args)

The functioning of this method is similar to the previous one. It consists of 2 parameters namely,
fmt – It specifies the format of the string.
Arguments – It is the arguments referenced by format specifier.

The return value of the method is similar to the previously discussed method. It also shows IllegalFormatException in the case of insufficient strings or incompatible arguments.

Let us see an example program using this method,

import java.io.Console;

public class Password {
  public static void main(String[] args) {

    Console c = System.console();
    String fmt = "%s %s %s";

    if (c == null) 
    {  
      System.out.println("Console Not Available!");
    }    
    else
    {
      //Reads user name
      String user = c.readLine(fmt, "Enter", "User", "Name: "); 
      
      //Reads password by masking
      char[] pass = c.readPassword(fmt,"Enter", "the", "Password: ");
      
      //Converts char array to String
      String password = String.valueOf(pass);
      
      //Displays user name
      System.out.println("The User Name is: " + user);
      //Displays Password
      System.out.println("The Password is: " + password);
    }
  }
}

Output

Enter User Name: Admin
Enter the Password:
The User Name is: Admin
The Password is: 12345

I hope this tutorial was helpful to you. You may also learn,

Password Validation Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *