从文本文件中读取并将用户输入与文本文件中的某些字段列匹配。列是分隔的

时间:2017-02-23 04:54:10

标签: java menu io delimiter

我正在使用Java创建基于菜单的控制台应用程序。我有一个类,允许用户通过输入他们的信息进行注册。此信息将写入每次输入新用户时附加的文件。

我想要一个登录功能,但我无法弄清楚如何阅读文件,只将用户输入与前两列ID;Password匹配。

一旦他们匹配用户输入,我就可以继续下一个菜单了。 我的文本文件如下所示:

ID;Password;FirstName;LastName;Email
admin;1234;adminFirst;adminLast;adminMail@admin.com

这里也是我的登录课程。我为用户输入创建了一个数组,以防万一有用:

public class Log_In extends Main_Menu {

    public void logging_in(){

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter your login information!");

        String [] log_in_array = new String[2];

        String ID, password;

        System.out.print("ID: ");
        ID = in.next();

        System.out.print("Password: ");
        password = in.next();

        //Stores the ID and PASSWORD to the array. Now we will compare the array to the txt file to find a match
        //Must match FIELD_ONE;FIELD_TWO
        log_in_array [0] = ID;
        log_in_array [1] = password;


        in.close();

    }
}

2 个答案:

答案 0 :(得分:0)

如果文件的结构是这样的

username pass data data
username pass data data
username pass data data... and so on

您可以将文件读入ArrayList,但跳过所有非用户名或传递的内容

ArrayList<String> userinfo = new ArrayList();
while (input.hasNext()){ //input is a Scanner object that is your file
    userinfo.add(input.next());//username
    userinfo.add(input.next());//pass
    input.next();//skip
    input.next();//skip
}

每个用户名和密码都将在

中成对使用
ArrayList
(username, pass, username, pass, username, pass,...)

然后查看用户名和密码是否匹配

int index = userinfo.indexOf(ID);
if (userinfo.get(index+1).equals(password))//the password comes right after the username in the list
    return true;

我假设这是为了练习,你通常会使用某种类型的数据库来输入用户名和密码

答案 1 :(得分:0)

您可以编写帮助方法来读取您的文本文件并比较用户提供的ID和密码,如下所示。

// The name of the file to open.
static String fileName = "myTextFliel.txt";

public static boolean myHelper(String id, String password) {
    // This will reference one line at a time
    String line = null;
    boolean retVal= false;
    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            //create a token based on 
            String [] token=line.split(";");
            // because you know first and second word of each line in 
            // given file is id and password 
            if (token[0].equals(id) && token[1].equals(password)){
                retVal=true;
                return retVal;
            }
        }   

        // Always close files.
        bufferedReader.close();         
    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
    }
    return retVal;
}


public void logging_in(){

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter your login information!");

    String [] log_in_array = new String[2];

    String ID, password;

    System.out.print("ID: ");
    ID = in.next();

    System.out.print("Password: ");
    password = in.next();

    //Stores the ID and PASSWORD to the array. Now we will compare the array to the txt file to find a match
    //Must match FIELD_ONE;FIELD_TWO
    log_in_array [0] = ID;
    log_in_array [1] = password;


    // Here you can call your helper method. 
    boolean foundMe =myHelper(log_in_array [0],log_in_array [1])
    if (foundMe==true){
     //do whatever u want to do
    }

    in.close();

}

通过更多的工作,您可以忽略标题行。