Java Console read password

Description

Java Console read password

import java.io.Console;

public class Main {
  public static void main(String[] args) {
    Console console = System.console();
    if (console == null) {
      System.out.printf("Console is NOT available.%n");
    }//  w w w  .  j av  a2  s  .  com

    String userName = console.readLine("User Name: ");
    char[] passChars = console.readPassword("Password: ");
    String passString = new String(passChars);
    if (passString.equals("hi")) {
      console.printf("Hello %s", userName);
    } else {
      console.printf("Invalid password");
    }
  }
}



PreviousNext

Related