Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with the following java program requirement. Any help is greatly appre

ID: 3659794 • Letter: N

Question

Need help with the following java program requirement. Any help is greatly appreciated!


Requirements Your program should take the name of a log file to process as a command line argument. If no argument is given, you should print an error/usage message. The log file should have one entry per line where each entry consists of two tokens separated by whitespace (such as tabs or spaces). These two tokens will be described here as the source and the destination. For example, in a simple web server log. the "source" might be the IP address of a client that requested a resource and the "destination" would be the local URL of the requested resource. It is an error if a log file line contains only one token. If a line has more than 2 tokens, the first two tokens should be treated as source and destination and the rest of the line ignored. Your program should first print a list of all unique sources, one per line. Each source should be followed by the total number of destinations related to that source, then the number of unique destinations, and finally the full list of all destinations for that source. (See sample output below.) This sources section should be followed by another section, separated by a blank line, in which the destination and sources roles are reversed. That is, in the second section, you should print all unique destinations, the total number of sources and the number of unique sources related to that destination, and finally the list of all source related to that destination.

Explanation / Answer

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;


public class ProcessFile {

public static void main(String args[]) throws Exception {

if(args != null && args.length >= 1) {
ProcessFile processor = new ProcessFile();
processor.process(args[0]);
} else
System.out.println("Please enter log file name");
}

public void process(String logFile) throws Exception {

//Read the file
FileInputStream fstream = new FileInputStream(logFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
HashMap sources = new HashMap ();
HashMap destinations = new HashMap ();
while ((strLine = br.readLine()) != null) {


StringTokenizer st = new StringTokenizer(strLine," "); //space as delimter
if (st.countTokens() > 0) {
String src =st.hasMoreTokens() ? st.nextToken() : null;
String dest = st.hasMoreTokens() ? st.nextToken() : null;

//Map sources and destinations into a map
if(src != null) {
sources.put(src, sources.get(src)+","+dest);
//Add check for unique sources/destinations - search in the string of returned by get
destinations.put(dest, destinations.get(dest)+","+src);
}

}
}
//Close the input stream
in.close();

//Now we have sources and destinations print them in whatever way u need.
if(!sources.isEmpty()) {
for(String key: sources.keySet()) {
System.out.println("Source Name: "+key+" ");
System.out.println("Destinations separated by ',' "+sources.get(key));
//Note : Calculate count of destinations using string tokenizer
}
}

if(!destinations.isEmpty()) {
for(String key: destinations.keySet()) {
System.out.println("Destination Name: "+key+" ");
//Note : Calculate count of Sources using string tokenizer
System.out.println("Sources separated by ',' "+destinations.get(key));
}
}
}



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote