#16
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    I tried something with my code. I created an edited text file that contains only the IP addresses. When I ran the program below, I got this output...

    Found 11 unique IP addresses.
    The most suspicious are:


    This is the correct output. I copied this code over, and ran it to see what number it would output for the actual file. 25. How can I "tell" Java that I only care about the IP addresses?


    Code:
    import java.io.*;
    import java.util.*;
    
    public class LogFileAnalysis
    {
    	
    	public static void main (String[] args)
    	{
    		
    		Scanner keys = new Scanner (System.in);
    		System.out.println("Enter a log file to be analyzed");
    		String file = "";
    		file = keys.nextLine();
    		File f = new File(file);
    		int value = 0;
    		
    		Map<String, Integer> map = new HashMap<String, Integer>(); 
    		
    		try 
    		{
    			
    			Scanner s = new Scanner(f);
    			
    			while(s.hasNextLine())
    			{
    				
    				String input = s.nextLine();
    				
    				map.put(input, value);
    				value++;
    				if(map.containsKey(input))
    				{	
    					map.put(input, map.get(input) + 1);
    				}
    				else
    				{
    					map.put(input, 1);
    				}
    			}
    			
    			
    			System.out.println("Found " + map.size() + " unique IP addresses.");
    			System.out.println("The most suspicious are:");
    		
    			s.close();
    		}
    		
    		catch (FileNotFoundException e) 
    		{
    			System.out.println("Couldn't open file");
    		}
    	}
    	
    }
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    How about this:

    Code:
    while(s.hasNextLine()) {
    				
    	String input = s.nextLine();
    	String[] data = input.split("] ");
    	String clientInfo = data[2];
    	String IPaddress = clientInfo.split(" ")[1];
    	System.out.println(IPaddress);
    	if(map.containsKey(IPaddress)) {	
    		map.put(IPaddress, map.get(IPaddress) + 1);
    	}
    	else {
    		map.put(IPaddress, 1);
    	}
    }
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    "tell" Java that I only care about the IP addresses
    Use an if statement to detect the data you care about.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    Thank you!

    I'm trying to match the top three most common IP addresses with how many times they have occurred.

    I know I can use

    Code:
    System.out.println(map.keySet());
    to get the IP addresses to show.

    And

    Code:
    System.out.println(map.values());
    to get the number of times each key has appeared.

    But how would I be able to only get the "top 3" most common IP addresses? I can't use an 'if statement' (if map.values is less than or greater than a certain value.)

    This is the output I'm speaking of...

    209.129.94.61 (10 erroneous accesses)
    207.42.179.85 (4 erroneous accesses)
    75.128.10.211 (3 erroneous accesses)


    The code above correctly matches the IP address with the number of erroneous accesses. But because a map doesn't have order to it, it's not in order.



    Originally Posted by Aurum84
    How about this:

    Code:
    while(s.hasNextLine()) {
    				
    	String input = s.nextLine();
    	String[] data = input.split("] ");
    	String clientInfo = data[2];
    	String IPaddress = clientInfo.split(" ")[1];
    	System.out.println(IPaddress);
    	if(map.containsKey(IPaddress)) {	
    		map.put(IPaddress, map.get(IPaddress) + 1);
    	}
    	else {
    		map.put(IPaddress, 1);
    	}
    }
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    get the "top 3"
    Make a list, sort it biggest first and take the first three entries.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    I tried using a set.

    Here is the code I used.

    Code:
    Set<Map> set = new HashSet<Map>();
    Code:
    set.add(map);
    Code:
    System.out.println(set.toString());
    The output is below

    Enter a log file to be analyzed
    small.log
    Found 11 unique IP addresses.
    The most suspicious are:
    [{75.128.10.211=3, 178.154.163.29=2, 221.192.199.35=1, 66.249.68.146=2, 72.14.199.153=2, 209.129.94.61=10, 207.46.204.244=1, 67.218.116.163=1, 207.42.179.85=4, 209.85.238.118=2, 66.249.65.120=1}]


    I tried using a list.

    Here is my code.

    Code:
    ArrayList<Set> list = new ArrayList<Set>();
    	list.add(set);
    System.out.println(list);
    I got the same output as above. I've been looking through the Java API for sorting methods for lists and sets, but haven't seen any. Is there a way to order them based on the values in the map?






    Originally Posted by NormR
    Make a list, sort it biggest first and take the first three entries.
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The Collections class has some sort() methods.
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    Why is it when I use

    Code:
    Collections.sort(list);

    I get a 'red line' under the word 'sort?'


    Originally Posted by NormR
    The Collections class has some sort() methods.
  16. #24
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you compile it and get a compiler error message to post?
  18. #25
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method sort(List<T>) in the type Collections is not applicable for the arguments (Map<String,Integer>)

    at LogFileAnalysis.main(LogFileAnalysis.java:51)


    Do I need to change String and Integer in my map?


    Originally Posted by NormR
    Can you compile it and get a compiler error message to post?
  20. #26
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you make a small, complete program that is compilable to show the problem?
    Or change the arg to the sort() method to a List.
    The method sort(List<T>) in the type Collections is not applicable for the arguments (Map<String,Integer>)
    The error message says a Map is not a List (the arg for the sort() method)
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    I did some more coding.

    Code:
    	for (Map.Entry<String, Integer> entry : map.entrySet()) 
    				{
    					
                	    System.out.println(entry.getKey() + " (" + entry.getValue() + " erroneous accesses)");
                	}
    Output

    75.128.10.211 (3 erroneous accesses)
    178.154.163.29 (2 erroneous accesses)
    221.192.199.35 (1 erroneous accesses)
    66.249.68.146 (2 erroneous accesses)
    72.14.199.153 (2 erroneous accesses)
    209.129.94.61 (10 erroneous accesses)
    207.46.204.244 (1 erroneous accesses)
    67.218.116.163 (1 erroneous accesses)
    207.42.179.85 (4 erroneous accesses)
    209.85.238.118 (2 erroneous accesses)
    66.249.65.120 (1 erroneous accesses)


    What I want to print out

    209.129.94.61 (10 erroneous accesses)
    207.42.179.85 (4 erroneous accesses)
    75.128.10.211 (3 erroneous accesses)


    How can I delete all but the three above?
  24. #28
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how can I delete all but the three above?
    I don't know how to get sorted data from a Map based on the values.

    Some ideas:
    Scan the values in the Map and find the keys for the top 3 values. Then scan it again and delete any keys not in the list of top three.

    Extract the key/values into a new class object, put the new object into a list and sort the list in descending order. Take the top 3 from the list.
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    2
    I got three IP addresses along with the erroneous accesses to print out. All I need to do now is put the map in order. I've tried a treeMap. Any other suggestions?
  28. #30
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Remove the data from the Map so it can be sorted based on the values vs the keys.

IMN logo majestic logo threadwatch logo seochat tools logo