2

I need a program that reads from multiple files and displays a report based on the date range entered by the user.Data in the files looks like:

[C445] ComputerName:FRONTOFFICE UserID:YB Yenae Ball Station 7A  LanId: | (11/23 17:01:55) | Client is disconnected from agent.
[C445] ComputerName:FRONTOFFICE UserID:YB Yenae Ball Station 7C  LanId: | (11/23 17:02:00) | Client is connected to agent.
[C7AE] ComputerName:KCUTSHALL-PC UserID:GO kcutshall Station 9901  LanId: | (11/23 13:33:06 - 11/23 13:33:41)|Client is disconnected from agent.

I need output like:

Computer Name   No.of disconnects
A                   4
B                   2
C                   1

i.e No. of disconnects for a particular computer name in descending order corresponding to the entered date range. I tried doing it but I'm not able to get a consolidated count for the computers and also m unable to do the sorting.The output I got is:

Computer Name No. of disconnects

A               1
A               1
A               2
B               1
B               2
C               1
C               1

Please help me on this. Here is the code I wrote

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Scanner;
import java.util.*;
import java.text.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class ReadZip{
    public static void main(String args[]) throws ParseException {
        try {
            Scanner input1=new Scanner(System.in);

            Scanner input2=new Scanner(System.in);
            System.out.println("Enter start date");
            String userDate1=input1.nextLine();
            System.out.println("Enter end date");
            String userDate2=input2.nextLine();
            DateFormat df = new SimpleDateFormat ("MM/dd");
            Date d1=df.parse(userDate1);
            Date d2=df.parse(userDate2);

            ZipFile zf=new ZipFile("C:\\Users\\Engineeir\\Desktop\\QoS_logs.zip");
            Enumeration entries=zf.entries();

            BufferedReader input=new BufferedReader(new InputStreamReader(
            System.in));
            while (entries.hasMoreElements()) {
                ZipEntry ze=(ZipEntry) entries.nextElement();

                BufferedReader br=new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                String line; String name;String compnames;int lines=0;
                while ((line=br.readLine())!=null) {

                    String[] st=line.split("\\|",-1);

                    if(st.length>1){
                        String dates=st[1];
                        String[] parts=dates.split("-");
                        SimpleDateFormat f=new SimpleDateFormat("(MM/dd");
                        String[] ob=parts[0].split(" ");
                        String finaldate=ob[1];

                        Date d3=f.parse(finaldate);

                        if(d3.after(d1) && d3.before(d2)){
                            compnames=getName(st);
                            if(line.contains("Client is disconnected from agent")==true)
                            {
                                //compnames=getName(st);
                                lines++;
                            }
                        System.out.println(compnames+"\t"+lines);}
                        else{break;}
                        //System.out.println(compnames+"\t"+lines);

                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getName(String[] st)
    {
        String[] name=st[0].split("\\:",-1);
        String[] comp=name[1].split("\\ ",-1);

        return(comp[0]);
    }
}
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83

2 Answers2

2

Instead of printing as you go, try accumulating the data in a map (key: computer name, value: number of disconnects). Then sort on the map keys and print the final totals.

Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
0

Try creating an object that represents a dataset, create a list of those datasets as well as a Comparator and call Collections.sort(list, comparator).

Alternatively put the datasets into a sorted collection like a TreeMap and use the number of connections as the key.

Note that if you want to combine the data of multiple files you'd have to get any previously created datasets and merge the data of the new file into them (e.g. get the dataset for A which might have a connection count of 1 and add 2 to it, if the current file contains A -> 2, etc.).

Thomas
  • 87,414
  • 12
  • 119
  • 157