I am parsing from a file where I have city names and distances to other cities in this format:
"San Antonio"
"San Francisco"
Washington
"San Francisco"--Weed [305]
"San Francisco"--"Wisconsin Dells" [2189]
Washington--"San Jose" [2909]
I think that the problem is that when I chop up the strings, it creates different objects and for some reason, the String.equals and/or the hashcode() gives different results. Because of this, the Map.get(key) returns null values. I however do not know how to fix this.
public class spanning {
private static HashMap<String, HashMap<String, Integer>> map = new HashMap<String, HashMap<String, Integer>>();
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new FileReader(args[0]));
String temp;
while ((temp = read.readLine()) != null) {
if (temp.contains("[")) {
String[] parts = temp.split("--");
String[] parts2 = parts[1].split(" \\[");
Scanner scan;
scan = new Scanner(parts2[1]).useDelimiter("\\D");
int o = scan.nextInt();
map.get(parts[0]).put(parts2[0], o);
} else {
map.put(temp, new HashMap<String, Integer>());
}
}
System.out.println("Finished!");
}
}