我正在从一个文件中解析,其中我有城市名称和到其他城市的距离,格式如下:
"San Antonio"
"San Francisco"
Washington
"San Francisco"--Weed [305]
"San Francisco"--"Wisconsin Dells" [2189]
Washington--"San Jose" [2909]
我认为问题在于,当我切碎字符串时,它会创建不同的对象,并且由于某种原因, String.equals 和/或 hashcode() 会给出不同的结果。因此, Map.get(key) 返回空值。然而,我不知道如何解决这个问题。
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!");
}
}