有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java登录和身份验证系统。txt文件

我正在尝试编写一个带有身份验证的简单登录系统。 我首先尝试给用户两种选择:登录或注册。 然后,如果用户选择注册,则信息将存储在中。然后使用txt文件与登录详细信息进行比较,以检查此用户是否存在或密码是否有效。 我遇到了一个问题,当我注册了两个或更多的“账户”时, 我可以登录除第一个帐户以外的其他帐户。 从“阅读”的角度来看,似乎缺少了一些东西。txt文件,查找数据

public static void main(String[] args) throws Exception {

    File file = new File("database.txt");
    PrintWriter output = new PrintWriter(new FileWriter(file, true));
    Scanner input = new Scanner(file);
    Scanner keyboard = new Scanner(System.in);
    Customers customer = new Customers();

    System.out.println("1. Login");
    System.out.println("2. Register");

    int option = keyboard.nextInt();

    switch (option) {
        case 1:
            System.out.println("LOGIN PAGE");
            System.out.println("Username: ");
            String inpUser = keyboard.next();

            if (input.next().equals(inpUser)) {
                System.out.println("Password: ");
                String inpPass = keyboard.next();
                if (input.next().equals(inpPass)) {
                    System.out.println("Login Succesful!");
                } else {
                    System.out.println("Password is incorrect.");
                }
            } else {
                System.out.println("Username is incorrect.");
            }
            break;

        case 2:
            System.out.println("REGISTRATION PAGE");
            System.out.println("Username: ");
            String username = keyboard.next();
            customer.setUsername(username);


            System.out.println("Password: ");
            String password = keyboard.next();
            customer.setPassword(password);

            System.out.println("You've successfully registered! You may login now.");

            output.println(customer.getUsername());
            output.println(customer.getPassword());

            output.close();
            break;

        default:
            System.out.println("1 OR 2 PLEASE");
    }
    keyboard.close();
    input.close();
}

客户代码

public Customers(){

}

public Customers(String username, String password) {
    this.username = username;
    this.password = password;
}

public String getUsername() {
    return username;
}

public String getPassword() {
    return password;
}

public void setUsername(String username) {
    this.username = username;
}

public void setPassword(String password) {
    this.password = password;
}

}


共 (1) 个答案

  1. # 1 楼答案

    您只需要检查用户输入是否等于您的列表中的第一行。txt文件

    if (input.next().equals(inpUser)){
    

    你需要检查每个条目。为此,需要迭代文件中的每一行。像这样的东西应该可以

    case 1:
    System.out.println("LOGIN PAGE");
    System.out.println("Username: ");
    String inpUser =  keyboard.next();
    boolean found = false;
    while(input.hasNext() && !found) { // Add a loop here to chek for every entry in the file
        if (input.next().equals(inpUser)){
            System.out.println("Password: ");
            String inpPass = keyboard.next();
            if (input.next().equals(inpPass)){
                System.out.println("Login Succesful!");
                found = true;
                break;
            }
        } 
    }
    if(!found) {
        System.out.println("Login incorrect!");
    }