Use Console class to read user input? : Console « JDK 6 « Java






Use Console class to read user input?

 


import java.io.Console;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    Console console = System.console();
    String username = console.readLine("Username: ");
    char[] password = console.readPassword("Password: ");

    if (username.equals("admin") && String.valueOf(password).equals("secret")) {
      console.printf("Welcome to Java Application %1$s.\n", username);

      Arrays.fill(password, ' ');
    } else {
      console.printf("Invalid username or password.\n");
    }
  }
}

   
  








Related examples in the same category

1.Java Console readLine
2.Password Prompting with java.io.Console
3.Console read Password
4.Console output with format
5.Printing text with the Console class
6.Masking a password with the Console class