2016-06-10 108 views
0

我有這個任務。 輸入是TreeMap中的TreeMap,無法從第二張地圖獲取值

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 

輸出是:

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) 

的問題是我不知道如何添加(第27行代碼爲trow異常,也不知道爲什麼)+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

我不得不猜測(我不會指望行,看看有什麼是第27行),但它可能是這一行:Integer tempCount = (map.get(user).get(ip)) + 1;。看看你的代碼:上面的3行將空的樹形圖放入map,用戶是關鍵。然後你爲某些ip添加一個計數。 但是如果該用戶的下一個IP是不同您現在可以嘗試從地圖獲取不存在的鍵的值,並向其添加1。

例子:

user = "ThomasGo" 
ip1 = "1.2.3.4" 

填補了地圖後,你就會有這樣的事情:

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

現在下面的調用應該會成功:

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

然而,考慮下一個呼叫使用不同的IP,例如「2.2.2.2」:

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

由於內部地圖不包含IP map.get("ThomasGo").get("2.2.2.2")將返回null。接下來,系統會嘗試解除空值以便爲其添加1,但拆箱失敗並帶有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