0

So I am completely stuck on a homework assignment for my java class. In it, I need to write a program that analyzes a web server's log file to determine which computers have attempted to access that web server the most. I have chosen to use a map to track the files that are ran with it in order to save each unique address with an integer value that would increase each time the code finds a duplicate IP address.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LogTester 
{
 public static void main(String[] args)
 {
    int individualCounter = 0;
    int ipcounter = 0;
    Map<String, Integer> ipCheck = new HashMap<String, Integer>();
    System.out.println("Enter a log file to be analyzed");
    Scanner ui = new Scanner(System.in);
    String filename = ui.nextLine();
    File name = new File(filename);
    try 
    {
        Scanner dataStore = new Scanner(name);
        while(dataStore.hasNextLine())
        {
            //get the entire line from the log file
            String line = dataStore.nextLine();

            //find the location of the word "client", then move 7 more spaces (see log file)
            int subscriptOfClient = line.indexOf("client");
            String s1 = line.substring(subscriptOfClient + 7);
            //we now have a smaller string
            //this string begins with the IP address
            //se we find the ending bracket and extra the ip
            int subscriptOfBracket = s1.indexOf("]");
            String s2 = s1.substring(0, subscriptOfBracket);
            ipcounter++;
            ipCheck.put(s2, individualCounter++);

            //print out the IP(in the assignment, put this into a map)
        }
        System.out.println("Found " + ipcounter + " unique IP addresses.");
        System.out.println("The most suspicious are: ");
        for(String key : ipCheck.keySet()){
             System.out.println(key);
         }
    } 
    catch (FileNotFoundException e) 
    {
        System.out.println("Could not open that file");
    }
 }
}

The issue I am facing is I am not sure how to increase the value of each individual IP address, so that I can later compare them for the three most frequent IP addresses (something I can already handle). With my individualCounter variable, I was hoping to be able to somehow coax that into the integer value, but as it stands now, it will always be the same as ipcounter due to how poorly I have it coded.

An example of what I am looking for is: 77.46.30.42 appears 59 times, 207.42.179.85 appears 46 times, and 85.114.128.137 appears 19 times.

One of my classmates has suggested I try set, but I feel like that would not really help me get any closer, as map already checks for duplicates.

Tyler
  • 1
  • 2
  • look at this, this would be a great help to you as i guess http://stackoverflow.com/questions/81346/most-efficient-way-to-increment-a-map-value-in-java – piyushj May 18 '16 at 05:58

1 Answers1

1

You can do something like this:

if(ipCheck.containsKey(s2)){
    ipCheck.put(s2, ipCheck.get(s2)+1);
}else{
    ipCheck.put(s2, 1);
}

What this does is to check if the IP is already in the map and if it is, it gets the previous count for this IP and it increments it by one, if it is not, it adds it to the map and sets its count to 1.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • The if statement responds to me with a "Invalid argument to operation ++/--" message. Are you sure that is formatted correctly? – Tyler May 18 '16 at 06:04
  • @Tyler Oh, you're right, I've edited my question to address that. – Titus May 18 '16 at 06:10
  • Alright, so now it is properly changing it a value of one for each individual IP. However it is not increasing it each time it runs across a duplicate. Would checking for duplicates be able to further increase the counter, or is it simply stuck at one? 209.129.94.61 for example pops up 10 times, but it only supplies 1. – Tyler May 18 '16 at 06:15
  • @Tyler yes, that is right. `209.129.94.61` appears 10 times probably because you've forgot to remove this line `ipCheck.put(s2, individualCounter++);`, make sure that the code that I've posted is the only one that adds entries in the map (the only one that calls `ipCheck.put(....)`). – Titus May 18 '16 at 06:20
  • What I mean is 209.129.94.61 appears 10 different times, so I need the duplicates to add on a counter so its total is 10, not 1. as the code is, with your helpful advice, it only reaches 1. So from here, is it possible to check for duplicates to allow it increase the value of ipCheck.get(s2) – Tyler May 18 '16 at 06:26
  • @Tyler You need to make sure that the IPs don't contain extra spaces and are formatted the same, `ipCheck.containsKey(s2)` returns `true` only if the `String`s are exactly the same. To remove extra leading or trailing spaces, you can use `trim()`, eg: `line = line.trim();`. – Titus May 18 '16 at 06:29
  • @Tyler That doesn't make sense. Can you edit your question to post the code with the changes that you've made ? – Titus May 18 '16 at 06:44
  • Yea I realize why it doesn't make sense. I'm trying to adjust it now. – Tyler May 18 '16 at 06:47