Created
March 22, 2017 12:58
-
-
Save anonymous/838ce1ff1a14e28a48c7df43a2a0a23c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* Created by Marina on 21.3.2017 г.. | |
*/ | |
public class UserLogs { | |
public static void main(String[] args) { | |
Scanner scanner=new Scanner(System.in); | |
//create data structure for the users with ips | |
TreeMap<String,LinkedHashMap<String, Integer>> usersIPs=new TreeMap<>(); | |
while(true){ | |
String input=scanner.nextLine(); | |
if("end".equals(input)){ | |
break; | |
}else{ | |
String[] parts=input.split(" "); | |
String ip=parts[0].split("=")[1]; | |
String messages=parts[1].split("=")[1]; | |
String username=parts[2].split("=")[1]; | |
LinkedHashMap<String,Integer> newData=new LinkedHashMap<>(); | |
//check if the user exists | |
if(usersIPs.containsKey(username)){ | |
//check if the linkedHashMap contains the ip, if not-add it, if yes- increment the value | |
//this line seems to be the problem | |
if(usersIPs.get(username).containsKey(ip)){ | |
int countUpdate=usersIPs.get(username).get(ip)+1; | |
newData.put(ip,countUpdate); | |
usersIPs.put(username,newData); | |
} | |
else { | |
newData.put(ip,1); | |
usersIPs.put(username,newData); | |
} | |
} | |
else{ | |
newData.put(ip,1); | |
usersIPs.put(username,newData); | |
} | |
} | |
} | |
//print the treemap | |
for (String user : usersIPs.keySet()) { | |
System.out.println(user+": "); | |
LinkedHashMap<String, Integer> ipAndCount = usersIPs.get(user); | |
for (Map.Entry<String,Integer> entry : ipAndCount.entrySet()) { | |
System.out.print(entry.getKey()+" => "+ entry.getValue()+", "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment