0

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!");
}

}

Slayahh
  • 373
  • 4
  • 14
  • `String.equals()` will compare the text in the case of Strings. Have you got an example of what failed exactly? – achAmháin May 01 '18 at 15:09
  • Yeah, I added a line boolean test = map.containsKey(parts[0]) above the map.get()and it returned false – Slayahh May 01 '18 at 15:11
  • If you print `parts[0]` and the keys from the map, does `parts[0]` exist in it? – achAmháin May 01 '18 at 15:12
  • 1
    You're putting the whole of `temp` into the hashmap as the key, not `parts[0]`- did you mean to do that? In that `else` - check what `temp` looks like just before you put it in – David Lavender May 01 '18 at 15:12
  • If `temp` doesn't contain a distance, it is just put in the map as ""San Antonio"" or "Washington". Going to try what @notyou said now. – Slayahh May 01 '18 at 15:16
  • I found the error I think, it appears that all of my input from cities have an extra whitespace after them, so I'm going to try to remove that and try again, thanks. – Slayahh May 01 '18 at 15:21

1 Answers1

0

I've ran your code and below is the map when it reach a row that prints "Finished":

map = {HashMap@634}  size = 4
 0 = {HashMap$Node@640} "" -> " size = 0"
 1 = {HashMap$Node@641} ""San Antonio"" -> " size = 0"
  key = ""San Antonio""
  value = {HashMap@647}  size = 0
 2 = {HashMap$Node@642} ""San Francisco"" -> " size = 2"
  key = ""San Francisco""
  value = {HashMap@649}  size = 2
   0 = {HashMap$Node@654} "Weed" -> "305"
   1 = {HashMap$Node@655} ""Wisconsin Dells"" -> "2189"
 3 = {HashMap$Node@643} "Washington" -> " size = 1"
  key = "Washington"
  value = {HashMap@651}  size = 1
   0 = {HashMap$Node@662} ""San Jose"" -> "2909"

What was the exact problem? Did you see what was the values of your map right before it goes out of the method?

Bilguun
  • 130
  • 1
  • 12