Java IO Tutorial - Java Console.readPassword(String fmt, Object ... args)








Syntax

Console.readPassword(String fmt, Object ... args) has the following syntax.

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

Example

In the following code shows how to use Console.readPassword(String fmt, Object ... args) method.

/* w w  w  .j a v  a2  s .  co  m*/
import java.io.Console;

public class Main {
  public static void main(String[] args) throws Exception {

    String fmt = "%2$5s %3$10s%n";

    Console cnsl = System.console();

    if (cnsl != null) {
      String alpha = cnsl.readLine(fmt, "Your", "Name: ");

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

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

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

  }
}

The code above generates the following result.