2016-06-10 2 views
0

이 작업이 있습니다.TreeMap의 TreeMap은 두 번째지도에서 값을 가져올 수 없습니다.

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. 

는 기본적으로 나는 그들이 형식으로 전송 한 모든 IP 얼마나 많은 메시지와 모든 사용자를 인쇄해야

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

문제는 I는 다음과 같습니다 입력

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 

출력이입니다 추가하는 방법을 모르겠다. (코드 27에서 행 27을 예외로하고 이유를 모른다.) +1을 IP로. 마지막 키의 마지막 값을 잡는 방법.

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(); 
     } 
    } 
} 

다음 테스트는 여기까지입니다. 입력 :

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 

출력 : 내가 생각해야 할 것

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

답변

1

하지만 아마이 줄이야 (I 라인 (27)이 무엇인지보기 위해 줄을 계산하지 않습니다) : Integer tempCount = (map.get(user).get(ip)) + 1;. 당신의 코드를 살펴보십시오. 위의 3 줄은 비어있는 트리 맵을 사용자가 키로 map에 넣습니다. 그런 다음 일부 IP에 대해 개수를 추가합니다. 그러나 해당 사용자의 다음 ip가 인 경우이 아닌 경우 이제지도에서 존재하지 않는 키의 값을 가져 와서 1에 추가하려고 시도합니다.

예 : 이제

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

다음 호출이 성공한다 :

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

의 생각

user = "ThomasGo" 
ip1 = "1.2.3.4" 

는지도를 작성 후에는 다음과 같이이있을 것이다 다른 IP를 사용하기위한 다음 호출, 예. "2.2.2.2"

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

내부지도 이후는 null을 반환 해당 IP map.get("ThomasGo").get("2.2.2.2") 포함되어 있지 않습니다. 다음으로 시스템은 1을 추가하기 위해 null 값을 unbox하려고 시도하지만 unboxing은 NullPointerException으로 실패합니다.

실제로 ip의 수를 얻었는지 확인하고, 내부지도에 1을 넣는 것이 아닌지 확인해야한다고 수정하십시오.

예 :

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

어떻게 마지막 키의 마지막 값을 잡을 수 있습니다.

적어도 귀하의 요구 사항을 정확하게 이해하면 실제로 쉽습니다. 꼭 그렇게 할 필요는 없습니다.그냥 각 IP 매핑을 인쇄하고 쉼표를 추가하십시오. 첫 번째 (부울 플래그를 사용할 수 있음)를 제외하고는 모두 System.out.println(); 대신 루프 뒤에 점과 다음 줄 바꿈을 인쇄하는 System.out.println(".");을 호출하십시오.

+0

도와 주셔서 감사합니다. :) ... 부정확 한 점에 대해 사과드립니다. – Phil