【发布时间】:2016-02-15 01:26:51
【问题描述】:
我正在制作一个基于控制台的应用程序,它会要求用户输入用户名和密码,将它们作为字符串存储到对象中,并将对象存储到基于引用的链表中。
有什么方法可以让用户输入密码时显示为“*****”而不是“12345”,而系统(显然)读取“12345”?
我有一个类具有适合用户输入的方法。
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
【问题讨论】:
-
标准
java.io.Console只能隐藏密码,但不能用星号屏蔽输入符号。从头开始用星号实现屏蔽可能很乏味(而且不便携)。所以看看 JLine (jline.sourceforge.net) 库。这个 sn-p 将帮助String password = new jline.ConsoleReader().readLine(new Character('*'));
标签: java