如何从控制台输入掩码密码?我正在使用Java 6.
我尝试过使用console.readPassword(),但它不起作用.一个完整的例子可能对我有帮助.
这是我的代码:
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test
{
public static void main(String[] args)
{
Console console = System.console();
console.printf("Please enter your username: ");
String username = console.readLine();
console.printf(username + "\n");
console.printf("Please enter your password: ");
char[] passwordChars = console.readPassword();
String passwordString = new String(passwordChars);
console.printf(passwordString + "\n");
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个NullPointerException ...
我刚开始用IntelliJ IDE学习Java.我知道有点C#所以逻辑是有道理的,但到目前为止有一件事我无法克服它.
我如何从控制台读取?在C#中,您可以轻松地阅读人类输入的内容Console.ReadLine().在Java中, System.console().readLine();对我不起作用并抛出一个NullPointerException.
我在这里错过了什么?
可能重复:
在命令行上隐藏输入
我正在制作一个密码安全检查程序,我的问题很奇怪,因为我的程序运行得很好.我想知道的是,是否有任何方法可以将输入到控制台的文本显示为在密码字段中显示的内容.即,在用户按下返回键之前,输入的单词将显示为"****".
我知道JFrame有一种JPasswordField方法,但我不认为这只是在使用时帮助我Scanner.
这是我的代码:
import java.util.Scanner;
public class SecurityCheckerMain {
static String enteredPassword;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your password: ");
enteredPassword = input.nextLine();
Checker tc = new Checker(enteredPassword);
tc.checkSecurity();
}
}
Run Code Online (Sandbox Code Playgroud)
public class Checker {
//number of chars : the Higher the Better("password must be greater than 8 chars"){done}
//combination of uppercase and lowercase{done}
//contains numbers{done}
//non repeated characters (every char is different ascii …Run Code Online (Sandbox Code Playgroud)