1

我的代码有问题。我需要从 txtfile 读取数据。用户将输入他的用户名和密码,系统将检查存储在文本文件中的信息进行验证,一旦成功,他将登录。但我有一个错误,我不知道如何继续。

public class existingCustomers {

    String username, password;

    public existingCustomers() {
        Scanner input = new Scanner(System.in);

        System.out.println("Welcome to Kreg Hotel booking System");
        System.out.println("Please login.");
        System.out.println("==============================================");
        System.out.println("Username: ");
        username = input.nextLine();
        System.out.println("Password: ");
        password = input.nextLine();
        new userPassOk(username, password);
    }

    private boolean userPassOk(String user, String pass) throws FileNotFoundException {
        Scanner s = new Scanner(new File("customerinfo.txt"));
        username = user;
        password = pass;
        while (s.hasNextLine()) {
            String[] userPass = s.nextLine().split(":");
            if (userPass[0].equals(user) && userPass[1].equals(pass))
                return true;
        }
        return false;
    }
}

我的错误是

C:\Users\MegaStore\Desktop\java testing\existingCustomers.java:18: error: cannot find symbol
        new userPassOk(username,password);
            ^
  symbol:   class userPassOk
  location: class existingCustomers
1 error
4

2 回答 2

3

userPassOk不是,是方法;所以你不应该通过使用来调用它new

由于它返回 a boolean,因此您需要在条件中使用它,如下所示:

if(userPassOk(username, password))
{
  // My logic here!
} else {
  // No entry for you!
}
于 2012-07-25T16:59:28.173 回答
0

编译错误告诉你问题出在哪里,就在第 18 行。

你有new userPassOk

userPassOk不是一个类,它是一个方法。删除新的,它会编译。

它不会工作,但它会编译......

于 2012-07-25T17:00:48.097 回答