Home Tutorial Java Corejava How to read and display password from the console

 
 

How to read and display password from the console
Posted on: April 10, 2010 at 12:00 AM
This tutorial demonstrate how to read and display password from the console.

Description:

Console class was introduced in jdk 1.6 This class help in taking the input from the console using its readPassword method . Here in this example this sample program it will ask to feed the password. Note when feeding the password it won't echo password but it accept and display later.

Code:

import java.io.*;

public class ReadDisplayPassword {

  public static void main(String[] args) {

    Console cons = System.console();
    if (cons == null) {
      System.out.println("Console Object is not available.");
      System.exit(0);
    else {
      String format = "%1$4s %2$4s %3$8s%n";
      char[] pwd = cons.readPassword(format, "Enter""user",
      "Password :");
      System.out.print("You have entered password : ");
      System.out.println(pwd);
    }
  }
}

Output:

Related Tags for How to read and display password from the console:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.