• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading from multiple files and sorting the count corresponding to different data items

 
Priya Lal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
}
}
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you edit your post and put your code in a code tag to make it easier to read?
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that lines gets reset to zero each time through the outer while loop, so you will never get a total, just the number of resets in each file.
why don't you use a Map with computer name as the key and the number of resets as the value and increment that value each time through the loop.
that poor compnames variable is working too hard. (edited to remove "I dont see how this program could produce output you described")

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, how about simplifying for now and removing all the date checks until you know your counts are correct.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops. forgot my rule of always searching to make sure same question wasn't double posted on another site:
http://stackoverflow.com/questions/9949103/how-to-sort-the-count-obtained-corresponding-to-different-data-items-in-java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!