1

I'm a high school student learning Java and I want to know how to change input text automatically into an asterisk in Scanner. This is for a simple log-in system I have made for a project. My code is

Scanner scan = new Scanner(System.in);

   boolean correctLogin = false;
   String username;
   String password;
   String enteredUsername;
   String enteredPassword;

   while(correctLogin != true){
       System.out.println("Enter Username: ");
       enteredUsername = scan.nextLine();

       System.out.println("Enter Password: ");
       enteredPassword = scan.nextLine();

       if(enteredUsername.equals("username") && enteredPassword.equals("passw00rd")){
           System.out.println("You have entered the correct login info");
           correctLogin = true; 
           break; 
       }
       else{
           System.out.println("Your login info was incorrect, please try again");
       }
   }

    System.out.println("You are now logged in, good job!");

I want it so that when I type the password, it will automatically change into an asterisk.

johnny
  • 21
  • 1
  • 3
  • If they type their password into the console, the letters will appear in the console as you type. Not sure what benefit youll gain from this. If you don't expect to use this from within an IDE, you could trt [`Console#readPassword`](https://docs.oracle.com/javase/8/docs/api/java/io/Console.html#readPassword-java.lang.String-java.lang.Object...-) – Vince Aug 14 '16 at 07:54

3 Answers3

2

try with this for password read:

Console console = System.console();
if(console != null){
  console.readPassword("Enter Password: ");
}
Riad
  • 3,822
  • 5
  • 28
  • 39
  • This will throw an exception when using the console from an IDE – Vince Aug 14 '16 at 08:04
  • When using javaw there most likely is no console. See https://stackoverflow.com/q/26470972/845117 – Dirk Schumacher Jun 06 '21 at 06:38
  • @Dioxin - correct. The code writer will have to make a choice. It is __impossible__ to pass-mask in IDEs that do not support readPassword on their console impls. There is no one right answer. Some apps will want to just revert to basic readLine, others will want to hard-exit at that point and inform the user they're going to have to find another solution. For either one, catch the exception and program the desired behaviour accordingly. – rzwitserloot Sep 15 '21 at 08:37
0

I also had the same issue with my console java application and I also do not want to display password in my IDE for security reasons. So, to find the bug I had to debug against the productive environment. Here is my solution that works for me in IntelliJ IDEA:


public static String getPassword() {

    String password;
    Console console = System.console();
    if (console == null) {
        password = getPasswordWithoutConsole("Enter password: ");
    } else {
        password = String.valueOf(console.readPassword("Enter password: "));
    }
    return password;
}

public static String getPasswordWithoutConsole(String prompt) {

    final JPasswordField passwordField = new JPasswordField();
    return JOptionPane.showConfirmDialog(
            null,
            passwordField,
            prompt,
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION ? new String(passwordField.getPassword()) : "";
}
-2

I am not sure if i get your question correctly, but still I will try to explain of what I understood.

To ensure that you see *** on the user screen you need to have some kind of a User interface written in HTML. I think in this case you are running your code in eclipse and via some kind of a main method. If that is the case then as Vince mentioned there is no benefit of ** since the letters would appear in the console.

What I would recommend is look for something basic web application tutorial and you would have more idea on how it works then.

HTH

user641887
  • 1,506
  • 3
  • 32
  • 50