How to disable console echoing

点点圈 提交于 2019-12-24 22:00:57

问题


I'm writing a command line program that makes use of println to display text in a very specific format. The problem I encounter is that every time I ask the user to enter something via Scanner.nextLine(), the console automatically echos their input in the console. Is there a way to disable the console from immediately displaying the user's entered text? In case it matters, I am running Linux.


回答1:


You can use the Console class and readPassword() method that comes with Java since version 6. That method disable the echo, but only reads array of chars that you must convert to whatever you need:

Console console = System.console();
if (console == null) {
    System.out.println("Console not active!");
    System.exit(0);
}

System.out.print("Type something, please: ");
// This won't echo anything 
char[] userInput = console.readPassword();


来源:https://stackoverflow.com/questions/32545424/how-to-disable-console-echoing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!