繁体   English   中英

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

[英]Reading from a text file and Matching user input to certain field columns in the text file. Columns are delimited

我正在使用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();

    }
}

如果文件的结构是这样的

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;

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

您可以编写帮助方法来读取您的文本文件并比较用户提供的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();

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM