I have a Java program which reads usernames and password histories from a text file and stores the info in a TreeMap. The user is then allowed to change passwords by selecting the username and then inputting their current password. The user info is initialized as:
TreeMap<String,LinkedList<String>> userInfo = new TreeMap<String,LinkedList<String>>();
The LinkedList is filled in this while loop:
while (filescan.hasNext()) {
String toParse = filescan.nextLine();
String[] parsed = toParse.split(" ");
username = parsed[0];
LinkedList<String> oldpws = new LinkedList<String>();
for (int i = 1; i < parsed.length; i++) {
oldpws.add(parsed[i]);
}
userInfo.put(username, oldpws);
}
The problem I'm running into is later on, comparing the password entered by the user with the last element of the LinkedList related to the given key. I can't use contains(currPW)
because that can return true for an invalid password.
if (/*last index of key's LinkedList*/.equals(currPW) {
//...
}
How can I access specifically the last index of the LinkedList as part of the TreeMap?
Some of the answers already point to the currect answer. But lets assume you have two input Strings with the username and password of a user you want to validate:
if (userInfo.contains(username)){
if(userInfo.get(username).getLast().equals(password){
return true;
} else {
//Bad password
return false;
}
}
//Unknown user
return false;