In Java I want to be able to have two JTextFields that take in String. One is a username and the other a password. to "login", the program will search through a file --> accounts.txt and will search first for "usrnm:", once it finds it, it takes the word right after the : the same goes for the password but it searches for "pswrd:".
Here's what iv'e got so far:
public void checkCredentials() {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("accounts.txt")));
String text = br.readLine();
text = text.toLowerCase();
text.split("usrnm:");
System.out.println("username: " + userInput);
} catch (Exception ex) {
ex.printStackTrace();
}
}
any help is appreciated!
Instead of searching the file twice I believe that its better to save first the username and on the next line the password. While you read them you can create User objects that will have the following attributes (a username and a password (String)). Also you could keep those into a linked list while your system is active. So that you could easily access the user accounts. Once a user needs to log in you will scan the list and by using the userList.get(i).equals(textfield.getText()) you will determine the user asked. Afterwards, if the above statement holds true you will check if the userList.get(i).getPassword().equals(textfield2.getText()) is true and grant or deny the access accordingly.
I am providing some useful parts below.
The userList is a dynamic linked list that uses generics
(ListInterface<User>)if you dont want to use generics you could just say ListInterface userList, whereever it appears.