屏蔽从控制台输入的密码,“无法解析符号'控制台'”

时间:2013-05-12 07:10:21

标签: java

因此,当我尝试编译此代码时,我得到:

java:找不到符号 符号:类控制台 location:类java.lang.System

我不明白为什么当我启动控制台对象时,System.console()无法解析...

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, '*');
  }

2 个答案:

答案 0 :(得分:13)

不是

Console console = new System.console();

应该是

Console console = System.console();

答案 1 :(得分:4)

您正在尝试使用System类访问控制台,其中console()方法是静态的,因此无需在System.console()前放置新关键字,只需删除新关键字。