Skip to content

Instantly share code, notes, and snippets.

@ansig
Created November 18, 2014 19:07
Show Gist options
  • Save ansig/53d5685ff4e9b58d0d2f to your computer and use it in GitHub Desktop.
Save ansig/53d5685ff4e9b58d0d2f to your computer and use it in GitHub Desktop.
Take in username and password from the console.
import java.io.Console;
/**
* Take in username and password from the console.
*/
public class ConsoleInput {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.printf("No console available, program not started in interactive shell%n");
System.exit(1);
}
String username = console.readLine("Enter your username: ");
char[] password = console.readPassword("Hello %s, please enter your password: ", username);
System.out.printf("The entered password was: %s%n", new String(password));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment