MK3GTX
MK3GTX

Reputation: 241

Masking password input from the console, "Cannot resolve symbol 'console'"

So when ever I try to compile this code, I get:

java: cannot find symbol symbol: class console location: class java.lang.System

I don't understand why when I initiate the console object, System.console() can't be resolved...

import java.io.Console;
import java.util.Arrays;
import java.util.Scanner;


public class User {
    Scanner input = new Scanner(System.in);
    Console console = new System.console();

    private String userID, userPW, userPIN;
    private char[] password = new char[6];


    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getUserPW() {
        return userPW;
    }

    public void setUserPW(String userPW) {
        this.userPW = userPW;
    }

    public String getUserPIN() {
        return userPIN;
    }

    public void setUserPIN(String userPIN) {
        this.userPIN = userPIN;
    }

 public void setUserInfo() {
      System.out.print("Please enter your User ID : ");
      this.setUserID(input.nextLine());

      System.out.print("Please enter your Password: ");
      this.setUserPW(input.nextLine());

      this.password = console.readPassword("Enter password");
      Arrays.fill(this.password, '*');
  }

Upvotes: 0

Views: 2221

Answers (2)

Sudhir Dhumal
Sudhir Dhumal

Reputation: 970

you are trying to access console using System class, where console() method is static so there is no need to put new keyword in front of System.console(), Just delete the new keyword.

Upvotes: 4

johnchen902
johnchen902

Reputation: 9599

It's not

Console console = new System.console();

It should be

Console console = System.console();

Upvotes: 13

Related Questions