Each time you call
list
you create a new
ArrayList
and add the user info to it:
public static ArrayList<String> list() throws IOException
{
...
ArrayList<String> arr = new ArrayList<String> ();
while ((line = reader.readLine()) != null)
{
arr.add(line);
}
reader.close();
return arr;
...
}
So each time you use it, you get one user's information and then throw it away!
As a result, the user is never in the collection next time you try to find it!
You need to do one of two things:
1) Create an ArrayList at class level (outside any method) and use that instead of creating a new one in
list
Or
2) Create an ArrayList in
main
and pass it to
list
each time you call it.
But ... you are really doing this very, very wrong.
By keeping usernames and passwords in the same collection, you can';t tell if I'm logging in as my password with by username, or logging in as my username with your password! Because your search will find a match if it locates any string: if I create a user "Griff" with password "Original", and your do the same with "Noney" and "Abees", Anyone can log in with any combination:
Griff Original
Griff Griff
Griff Noney
Griff Abees
Original Original
Original Griff
Original Noney
Original Abees
Noney Original
Noney Griff
Noney Noney
Noney Abees
Abees Original
Abees Griff
Abees Noney
Abees Abees
All would be valid combinations for a successful login.
What you need to do if create either separate collections for names and passwords and compare the password only with the same index as the required username; or better create a User class which contains the username and password, then search for that in your collection instead of the "raw strings".