Java Console.readPassword()

Syntax

Console.readPassword() has the following syntax.

public char[] readPassword()

Example

In the following code shows how to use Console.readPassword() method.


import java.io.Console;
//from  w  w w . j a  v a2s.  c o m
public class Main {
  public static void main(String[] args) throws Exception {

    Console cnsl = System.console();

    if (cnsl != null) {

      String alpha = cnsl.readLine("Name: ");

      System.out.println("Name is: " + alpha);

      char[] pwd = cnsl.readPassword("Password: ");

      System.out.println("Password is: " + pwd);
    }
  }
}

The code above generates the following result.