-1

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?

vroom
  • 45
  • 1
  • 5
  • 3
    Have you tried `#getLast()`? – Dan Grahn May 01 '15 at 16:02
  • Make sure you salt and hash those passwords! https://crackstation.net/hashing-security.htm – Dan Grahn May 01 '15 at 16:05
  • Oh, I didn't think I could use `getLast()` in this context, but I've got it now with `userInfo.get(username).getLast().equals(currPW)`. Sorry if that was obvious, I had made an incorrect assumption about the TreeMap. Thanks for the help. – vroom May 01 '15 at 16:18

2 Answers2

0

You can get the last element like this.

final LinkedList<String> passwords = userInfo.get(username);
final String currentPassword = passwords.getLast();

And compare them like this.

if(currPW.equals(currentPassword) {
  // Login
}
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
  • That would work if it was just the LInkedList, but I'm working with the TreeMap `userInfo`, and I don't know how to access specific elements of the list in the context of a TreeMap. – vroom May 01 '15 at 16:12
  • Just added it. That's a fairly simple operation. – Dan Grahn May 01 '15 at 16:13
0

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;
gfelisberto
  • 1,655
  • 11
  • 18
  • Ok, that's what I'm using now. I had for some reason assumed that I couldn't access the LinkedList in the same way when it was part of the TreeMap. Thank you – vroom May 01 '15 at 16:22
  • The TreeMap is just a place where you put things. In this case you are putting LinkedLists there and cataloging them with a String. When you get() something from the Map you what you have placed there. – gfelisberto May 01 '15 at 16:27
  • `if(true) return true else return false` – nachokk May 01 '15 at 16:36