2

I'm making a console based application that will ask from the user ton enter a username and password, store them into an object as strings, and store the object into a reference based linked list.

Is there any way that when the user types his password it will appear as "*****" instead of "12345" while the system (obviously) reads "12345"?

I have a class with methods appropriate for user input.

System.out.println("Username: ");
String un = EasyIn.getString();
System.out.println("Password: ");
String pw = reader.getString();
System.out.println("Repeat Password: ");
String pwRepeat = reader.getString();
//and then go on to confirm that the passwords match and store them in the object etc
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Ema Black
  • 63
  • 1
  • 12
  • Standard `java.io.Console` can only hide password but it can not mask input symbols with asterisks. Implementing masking with asterisks from scratch can be tedious (and not portable). So take a look at JLine (http://jline.sourceforge.net) library. This snippet will help `String password = new jline.ConsoleReader().readLine(new Character('*'));` – flaz14 Nov 13 '15 at 18:04

1 Answers1

2

You can use Console class

http://docs.oracle.com/javase/6/docs/api/java/io/Console.html

and the read password method: http://docs.oracle.com/javase/6/docs/api/java/io/Console.html#readPassword()

The readPassword method provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled.

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40