【问题标题】:How to hide and turn my input into asterisk in Java [duplicate]如何在Java中隐藏我的输入并将其转换为星号[重复]
【发布时间】:2021-12-16 15:04:58
【问题描述】:

我是一名 ICT 学生,我需要帮助。我们的教授要求我们创建一个登录系统。我已经做了一个,但问题是输入应该是隐藏的。我该怎么做?

例如,

import java.util.Scanner;
int password;
Scanner x = new Scanner(System.in);
password = x.nextInt();

我需要隐藏输入,并将该消息转换为星号“*”。有点 Facebook 的做法。

【问题讨论】:

    标签: java input user-input hide helper


    【解决方案1】:

    this solution

    即:

    Console console = System.console();
    
    String enteredPassword = new String(console.readPassword("Please enter your password: "));
            
    System.out.println("You entered: " + enteredPassword);
    

    【讨论】:

      【解决方案2】:

      祝你好运,项目顺利。
      所以,我说放一个复选框来改变一些布尔变量的值,比如flag,其余的是这样的:

          String user_name;
          String pass;
          boolean flag;
      
          void checkBoxPropertyChanged(Action e) {
              flag =! flag;
              changePasswordField();
          }
      
      
      
          void changePasswordField() { 
              String hidden = ""; 
              int len = pass.length();
      
              for(int i = 0; i<len; i++) 
                  hidden += "*"; 
              passwordField.setText((flag) ? pass : hidden); 
          }
      

      【讨论】:

        猜你喜欢
        • 2022-08-03
        • 1970-01-01
        • 2020-09-20
        • 1970-01-01
        • 1970-01-01
        • 2015-03-31
        • 2016-06-18
        • 2013-11-17
        • 1970-01-01
        相关资源
        最近更新 更多