有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

树映射中的java树映射,无法从第二个映射中获取值

我有这个任务。 输入是

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)

问题是我不知道如何将+1添加到IP(代码trow异常中的第27行,也不知道为什么)。 以及如何捕捉最后一个键的最后一个值

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) 个答案

  1. # 1 楼答案

    我不得不猜测(我不会数数第27行是什么),但可能是这一行:Integer tempCount = (map.get(user).get(ip)) + 1;。看看你的代码:在上面的3行中,你把一个树映射放到map中,用户是键。然后你为一些ip添加一个计数但是如果该用户的下一个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);
    }
    

    And how to catch last value of last key.

    这其实很容易,至少如果我正确理解你的要求:你不需要。只需打印每个ip映射,并在除第一个之外的所有ip映射之前添加一个逗号(可以使用布尔标志),然后在循环之后调用System.out.println();,而不是System.out.println(".");,它打印一个点,然后是一个换行符