首页 > 解决方案 > Java - 模拟登录控制台 - 在询问密码之前验证用户名

问题描述

(格式已关闭,无法发布我如何从 Eclipse 复制和粘贴)

我试图在继续询问密码之前验证用户名,我尝试在服务子类中拆分用户名和密码,但这没有用。现在,即使用户名没有存储在 txt 文件中,它也会从用户名直接询问密码,我有它阅读表单。这是我到目前为止所拥有的:

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class LoginConsole {
public static User[] loginUsers = new User[4];
private static UserService userService = new UserService();

public static void main (String[] args) throws IOException {
        ReadFile();
        
        Scanner input = null;
        
        try {
                input = new Scanner(System.in);
                
                boolean validInput = false;
                int attempts = 0;
                while (!validInput && attempts != 5) {
                        System.out.println("Enter your username:");
                        String username = input.nextLine();
                        System.out.println("Enter your password:");
                        String password = input.nextLine();
                        
                        User found = userService.yesFound(username, password);
                        if (found != null) {
                                System.out.println("Welcome: " + found.getName());
                                validInput = true;
                        break;
                                
                        } if(attempts < 4) {
                                System.out.println("Invalid input, please try again!");
                                attempts++;
                        }   else {
                                        System.out.println("Too many failed attempts, you are 
                                          now locked out!");
                        break;      
                        }
                    }
                } finally {
                        if (input != null)
                         input.close();
                }
            }
            
            private static void ReadFile() throws IOException, FileNotFoundException {
            String verInput = "data.txt";
            BufferedReader reader = null;
            
            try {
                    reader = new BufferedReader(new FileReader(verInput));
                    
                    String currLine;
                    int i = 0;
                    while ((currLine = reader.readLine()) != null) { 
                        loginUsers[i] = new User(currLine.split(","));
                        i++;
                    }
            } finally {
                    if (reader != null)
                        reader.close();
    }
    
  }
}   

标签: javaeclipse

解决方案


由于您尝试在密码之前验证用户名,因此您应该在询问密码之前进行验证。

System.out.println("Enter your username:");
String username = input.nextLine();

// Validate Username here. (example ..)
User user = userService.findUser(username); // you can create a method dedicated to find user.

if(user != null) {
   System.out.println("Enter your password:");
   String password = input.nextLine();
   
   ...
}

推荐阅读