简体   繁体   中英

Java delimiter on a text file

I am trying to read a txt file with this format (heading not included):

      firstname             lastname             username             password 
           John                  Doe             $1234567                 $123
           Eden                 Hask             $1234576               $12345

The latest login (Eden) would always work but not the one prior(John). For some reason, the delimiter would include all details until the next $. How should I get my delimiter to stop after the password.

The user name is_1234567 and the password is_123
                Eden                 Hask!

The user name is_1234576 and the password is_12345!

Here is the entire code.

public void login() throws FileNotFoundException{  
        File file = new File("file.txt");
        Scanner read = new Scanner(file);
        found = false;

        read.useDelimiter("(\s*\\$)");

        try {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Username: ");
            String username = keyboard.nextLine();
            System.out.print("Password: ");
            String passwordField = keyboard.nextLine();
            while(read.hasNext()){
                String user = read.next();
                String pass = read.next();
                System.out.println("The user name is_" + user + " and the password is_" + pass + "!\n");
                    if(user.trim().equals(username) && pass.trim().equals(passwordField)){
                        boolean found = true;
                        break;  
                    }        
                }
             if(found){
                return true;
             }
             else {
                return false;
             }
            read.close();
        }catch(Exception e){
            throw e;
        }
    };

From what you have described it looks like you also want to break your string when a line ends. To do this add a carriage return as one of delimiting cases in the following manner-

read.useDelimiter("\n|(\s*\\$)")

this should prevent all the previous 'password' strings from including details from the next line

I would do it completely different:

boolean found;
    public void login() throws FileNotFoundException {
        File file = new File("file.txt");
        Scanner read = new Scanner(file);

        try {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Username: ");
        String username = keyboard.nextLine();
        System.out.print("Password: ");
        String password = keyboard.nextLine();
        
        // Skip the first line
        read.nextLine();
        
        while (read.hasNextLine()) {
            String line = read.nextLine();
            //split current line with spaces into an array
            String[] lineArray = line.split("\\s+");
            String user = lineArray[1];
            String pass = lineArray[4];
            System.out.println("The user name is " + user + " and the password is " + pass + "!\n");
            if (user.equals(username) && pass.equals(password)) {
                found = true;
                break;
            }
        }
        read.close();
        keyboard.close();
        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

In addition to the line break inclusion in the delimiter, you may want to read the name of the user to keep the userid/password reads in sync:

    read.useDelimiter("(\\s*\\$)|(\\R)");

    try {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Username: ");
        String username = keyboard.nextLine();
        System.out.print("Password: ");
        String passwordField = keyboard.nextLine();
        while(read.hasNext()){
            String name = read.next();
            String user = read.next();
            String pass = read.next();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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