简体   繁体   English

使用Java中的while循环将用户输入转换为星号(如密码)

[英]convert user input into asterisk like password using while loop in java

Using while loop in java how can i convert user input into asterisk while it is being typed like in password. 在Java中使用while循环时,如何像键入密码一样将用户输入转换为星号。 It is possible and how? 有可能,如何? Thanks. 谢谢。

        System.out.print("Enter password: ");

        while(true)
        {
            password = pal.nextLine();
            password = "*"; 
            System.out.print(password);
        }

This is the only idea that i have but it doesn't work 这是我唯一的想法,但是不起作用

Not sure about displaying asterisks but you could make the password invisible by using the Console.readPassword method 不确定是否显示星号,但可以使用Console.readPassword方法使密码不可见

public class Main {    

  public static void main(String[] args) {
    char passwordArray[] = System.console().readPassword("Enter password: ");
     // entered password will be stored in this array        
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM