Stupid error by me. The string key I was passing in had parenthesis and I didn't realize that. I added a .replace() to the key and everything is good now. Thanks for the responses.
So I have a class that reads a csv file that contains nfl players name, position, salary, points, and team. The DKdata class reads the file, getPlayers method returns the map. My problem I am having is that whenever I try to use get(key) it only returns null. I read online something about equals method overriding but I am not sure how to implement it for this. If anyone can help me out or lead me in the right direction that will be greatly appreciated. Code below with output.
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[]args) throws FileNotFoundException {
Map<String, Player> players = new HashMap<String, Player>();
DKdata d = new DKdata();
//players.putAll(d.getPlayers());
Player p = new Player("WR","Kev", 1000, 99, "Pit");
String name = p.name;
players.put(p.name, p);
System.out.println(players.get(name).salary);
players.putAll(d.getPlayers());
System.out.println(players.get("Zach Ertz").salary);
}
}
public class DKdata {
private Map<String, Player> players;
private Scanner scanner = new Scanner(new File("/Users/kevinrhea/Documents/DraftKing/DKsalaries.csv"));
public DKdata() throws FileNotFoundException {
try {
players = new HashMap<String, Player>();
scanner.useDelimiter(",");
scanner.nextLine();
while(scanner.hasNext()){
String[] data = scanner.nextLine().split(",");
Player player = new Player(data[0], data[1], Integer.parseInt(data[2]), Double.parseDouble(data[4]), data[5]);
players.put(data[1], player);
}
} catch(Exception e) {
e.printStackTrace();
}
}
public Map<String, Player> getPlayers(){
return players;
}
}
Output:
{"Zach Ertz"=Player@2503dbd3, "Jacoby Brissett"=Player@4b67cf4d, "Brandon Bolden"=Player@7ea987ac, ...
null
players.get("Alshon Jeffery")returns null, this means you don't have that key in your Map. You don't have to overrideequals, since your key is a String, which already overridesequals.scanner.nextLine();statement before yourwhileloop.NullPointerExceptions if the entries do not exist.) --> Please read How to create a Minimal, Complete, and Verifiable example and provide consistent information.