Search code examples
javatreemap

TreeMap in TreeMap , can't get value from second map


I have this task. Input is

IP=192.23.30.40 message='Hello&derps.' user=destroyer
IP=192.23.30.41 message='Hello&yall.' user=destroyer
IP=192.23.30.40 message='Hello&hi.' user=destroyer
IP=192.23.30.42 message='Hello&Dudes.' user=destroyer
end

Output is:

child0: 
192.23.33.40 => 1.
child1: 
192.23.30.40 => 1.
destroyer: 
192.23.30.42 => 2.
mother: 
FE80:0000:0000:0000:0202:B3FF:FE1E:8329 => 1.
unknown: 
192.23.155.40 => 1.
yetAnotherUsername: 
192.23.50.40 => 2.

Basically I have to print every user with every IP and how many messages they have sent in format

username: 
IP => count, IP => count… (last must be with dot in end)

The problem is I don't know how to added (row 27 in code trow exception, also don't know why) +1 to IP. And how to catch last value of last key.

package notReady;

import java.util.Scanner;
import java.util.TreeMap;

/**
 * Created by Philip on 10-Jun-16.
 */
public class Problem09_02_UserLogs {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TreeMap<String, TreeMap<String, Integer>> map = new TreeMap<>();

        while (true) {
            String in = scanner.nextLine();
            if (in.equals("end")) {
                break;
            }
            String[] input = in.split(" ");
            String ip = input[0].replaceAll("IP=", "");
            String user = input[2].replaceAll("user=", "");

            if (!map.containsKey(user)) {
                map.put(user, new TreeMap<>());
                map.get(user).put(ip, 1);
            }else {
                Integer tempCount = (map.get(user).get(ip)) + 1;
                map.get(user).put(ip, tempCount);
            }
        }

        for (String person : map.keySet()) {
            System.out.printf("%s: \n", person);
            for (String ips : map.get(person).keySet()) {
                System.out.printf("%s => %d.", ips, map.get(person).get(ips));
            }
            System.out.println();
        }
    }
}

This is next test, here everything is ok. input:

IP=FE80:0000:0000:0000:0202:B3FF:FE1E:8329 message='Hey&son' user=mother
IP=192.23.33.40 message='Hi&mom!' user=child0
IP=192.23.30.40 message='Hi&from&me&too' user=child1
IP=192.23.30.42 message='spam' user=destroyer
IP=192.23.30.42 message='spam' user=destroyer
IP=192.23.50.40 message='' user=yetAnotherUsername
IP=192.23.50.40 message='comment' user=yetAnotherUsername
IP=192.23.155.40 message='Hello.' user=unknown
end

Output:

destroyer:
192.23.30.40 => 2, 192.23.30.41 => 1, 192.23.30.42 => 1.

Solution

  • I'd have to guess (I won't count lines to see what is line 27) but it's probably this line: Integer tempCount = (map.get(user).get(ip)) + 1;. Have a look at your code: 3 lines above you put an empty treemap into map with the user being the key. Then you add a count for some ip. But if the next ip for that user is a different one you'll now try to get a value for a non-existant key from the map and add 1 to it.

    Example:

    user = "ThomasGo"
    ip1 = "1.2.3.4" 
    

    After filling the maps you'd have something like this:

    Map[key:"ThomasGo"->Map[key:"1.2.3.4"->1]]
    

    Now the following call should succeed:

    Integer tempCount = map.get("ThomasGo").get("1.2.3.4") + 1;
    

    However, consider the next call to use a different ip, e.g. "2.2.2.2":

    Integer tempCount = map.get("ThomasGo").get("2.2.2.2") + 1;
    

    Since the inner map doesn't contain that ip map.get("ThomasGo").get("2.2.2.2") would return null. Next the system would try to unbox the null value in order to add 1 to it but that unboxing fails with a NullPointerException.

    To fix that you'd have to check whether you actually get a count for the ip and if not just put 1 into the inner map.

    Example:

    Map<String, Integer> ipMap = map.get("ThomasGo");
    
    //no map found for the username
    if( ipMap == null ) { 
     //create a new inner map and put it into the outer map and keep the reference for further use
     ipMap = new TreeMap<>();
     map.put("ThomasGo", ipMap);
    }
    
    //check whether the ip is in the inner map
    Integer ipCount = ipMap.get(ip);
    if( ipCount == null) {
      //no count in the map so just put 1
      ipMap.put(ip, 1);
    } 
    else {
      //already a count in the map so overwrite it with count + 1
      ipMap.put(ip, ipCount  + 1);
    }
    

    And how to catch last value of last key.

    That's actually easy, at least if I understand your requirement correctly: you don't need to. Just print each ip mapping and add a comma before all except the first (you can use a boolean flag for that) and after the loop instead of System.out.println(); you call System.out.println("."); which prints a dot and then a line break.