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