Java delimiter on a text file

Viewed 109

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;
        }
    };
3 Answers

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();
    }
}

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

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();
Related