java - Comparing two lines in text file & printing single line corresponding to similar dates -


i want take line 1 , line 5 has username , date same in line 1 contains in time,and in line 5 contain out time want read 2 lines , compare check whether both lines have same username , date , if print single line in other text file or in hash map

example : "sangeetha-may 02, 2013 , -in-09:48:06:61 -out-08:08:19:27 (in java)

this content of text file :

line 1. "sangeetha-may 02, 2013 , -in-09:48:06:61 line 2. "lohith-may 01, 2013 , -out-09:10:41:61 line 3 . "sushma-may 02, 2013 , -in-09:48:06:61 line 4. "sangeetha-may 01, 2013 , -out-08:36:38:50 line 5. "sangeetha-may 02, 2013 , -out-08:08:19:27 line 6. "sushma-may 02, 2013 , -out-07:52:13:51 line 7. "sangeetha-jan 01, 2013 , -in-09:27:17:52-out-06:47:48:00 line 8. "madhusudhan-jan 01, 2013 , -in-09:38:59:31-out-07:41:06:40 

above data generating using code below

import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.util.hashset; import java.util.hashmap; import java.util.iterator; import java.util.set; import java.util.map; import java.util.treemap;  public class flatfileparser  {     public static void main(string[] args)     {             // stream we're reading         bufferedreader in;         bufferedwriter out1;          bufferedwriter out2;         // return value of next call next()         string nextline;         try          {             if (args[0].equals("1"))             {                 in = new bufferedreader(new filereader(args[1]));                 nextline = in.readline();                 while(nextline != null)                 {                     nextline = nextline.replaceall("\\<packet","\n<packet");                     system.out.println(nextline);                     nextline = in.readline();                 }                 in.close();             }             else             {                 in = new bufferedreader(new filereader(args[1]));                 out1 = new bufferedwriter(new filewriter("invalues.txt" , true));                  out2 = new bufferedwriter(new filewriter("outvalues.txt"));                 nextline = in.readline();                 hashmap<string,string> inout = new hashmap<string,string>();                 while(nextline != null)                 {                     try                     {                         if (nextline.indexof("timetracker")>0)                         {                             string = "";                             string indate = "";                             if (nextline.indexof("of in")>0)                             {                                  int posfrom = nextline.indexof("from");                                 int posnextat = nextline.indexof("@", posfrom);                                 int posts = nextline.indexof("timestamp");                                 = nextline.substring(posfrom+5,posnextat);                                 indate = nextline.substring(posts+11, posts+23);                                 string dd = indate.split(" ")[1];                                 string key = dd+"-"+from+"-"+indate;                                 //string key = from+"-"+indate;                                 string intime = "-in-"+nextline.substring(posts+24, posts+35);                                 inout.put(key, intime);                                  }                             else if (nextline.indexof("of out")>0)                             {                                 int posfrom = nextline.indexof("from");                                 int posnextat = nextline.indexof("@", posfrom);                                 int posts = nextline.indexof("timestamp");                                 = nextline.substring(posfrom+5,posnextat);                                 indate = nextline.substring(posts+11, posts+23);                                 string dd = indate.split(" ")[1];                                 string key = dd+"-"+from+"-"+indate;                                 string outtime = "-out-"+nextline.substring(posts+24, posts+35);                                 if (inout.containskey(key))                                 {                                     string val = inout.get(key);                                     if (!(val.indexof("out")>0))                                         inout.put(key, val+outtime);                                                     }                                 else                                     inout.put(key, outtime);                             }                         }                     }                     catch(exception e)                     {                         system.err.println(nextline);                         system.err.println(e.getmessage());                     }                     nextline = in.readline();                     }                 in.close();                  for(string key: inout.keyset())                 {                     string val = inout.get(key);                     out1.write(key+" , "+val+"\n");                     system.out.println(key + val);                 }                 out1.close();             }         }          catch (ioexception e)         {             throw new illegalargumentexception(e);         }     } } 

description: these log in , log out times of employees,i reading these log file ,but coming in single line line 7 , line 8 , coming in different lines same date,i want print in same line example have provided above, , ever records coming in single line both in , out time ishould retain is.... plz can ....!

considering have list of lines file in lstfile.

you can this

string output="",line1,line2; for(int i=0;i<lstfile.size();i++) {      line1=lstfile.get(i);     if(line1.contains("in") && line1.contains("out"))continue;     for(int j=i+1;j<lstfile.size();j++)     {         line2=lstfile.get(j);          if(line2.contains("in") && line2.contains("out"))continue;          if(line1.contains(getnamedate(line2)) && line2.contains("out") && line1.contains("in"))         {               output+=line1+line2.substring(line2.lastindexof(","),line2.length());               output+=system.getproperty("line.separator");               break;         }     } } //output contains desired result 

the below method name , date

public string getnamedate(string input) {     return input.substring(0,input.lastindexof(",")); } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -