i reading text file contains log in , out values of employees, , trying display in single line bot in , out values in different line same employee , date.the data in text file shown below.
line 1. 02,-"sangeetha-may 02, 2013 , -in-09:48:06:61 line 2. 01,-"lohith-may 01, 2013 , -out-09:10:41:61 line 3. 02,-"sushma-may 02, 2013 , -in-09:48:06:61 line 4. 01,-"sushma-jan 01, 2013 , -in-09:07:06:50-out-05:39:01:63 line 5. 02,-"sangeetha-may 02, 2013 , -out-08:08:19:27 line 6. 02,-"sushma-may 02, 2013 , -out-07:52:13:51 line 7. 03,-"lohith-may 03, 2013 , -in-11:39:44:08
example: line 1 , line5 in , out values same employee sangeetha, should display like:
02,-"sangeetha-may 02, 2013 , -in-09:48:06:61, -out-08:08:19:27
perhaps getting output, line 2, don't have in value, code not able displaying ever employee having both in , out values in different displaying those. want display theses records missing message appended it..my code this..
public class recordparser { public static void main(string[] args) { recordparser rp = new recordparser(); rp.recordformatter("sample.txt"); } public static void recordformatter(string filename) { try { bufferedreader in; list<string> ls = new arraylist<string>(); string line = ""; string line1; string line2; string lines; string mergedrecords = ""; string normalrecords = ""; string halfrecords = ""; in = new bufferedreader(new filereader(filename)); while ((line = in.readline()) != null) { ls.add(line); } (int = 0; < ls.size(); i++) { line1 = ls.get(i); if (line1.contains("in") && line1.contains("out")) { normalrecords += line1; normalrecords += system.getproperty("line.separator"); // ls.remove(i); // break; } (int j = + 1; j < ls.size(); j++) { line2 = ls.get(j); if (line2.contains("in") && line2.contains("out")) continue; if (line1.contains(getnamedate(line2))) { mergedrecords += line1 + line2.substring(line2.lastindexof(","), line2.length()); mergedrecords += system.getproperty("line.separator"); // ls.remove(i); // ls.remove(i); break; } if (!line1.contains(getnamedate(line2))) { if (!mergedrecords.contains(getnamedate(line1))) { halfrecords += line1; halfrecords += system.getproperty("line.separator"); } } } } system.out.println(mergedrecords); system.out.println(normalrecords); system.out.println(halfrecords); // && line2.contains("out") && line1.contains("in") } catch (exception e) { system.err.println(e.getmessage()); } } public static string getnamedate(string input) { return input.substring(0, input.lastindexof(",")); } }
please can modify show records having either in entry or out entry?
for example..line 2 should display like:
line 2. 01,-"lohith-may 01, 2013 ,missing in, -out-09:10:41:61..
presently getting output is:
02,-"sangeetha-may 02, 2013 , -in-09:48:06:61, -out-08:08:19:27 02,-"sushma-may 02, 2013 , -in-09:48:06:61-out-07:52:13:51 01,-"sushma-jan 01, 2013 , -in-09:07:06:50-out-05:39:01:63
i want line 2 record display these.
the biggest problem code is 1 monolithic mess. it's hard see you're going wrong.
i put code process input. reason wrote code show , others reading later how break monolithic code tiny, testable pieces.
not including input string created (since have input file), there no method in code longer 20 lines. shorter.
i ran code 3 times. first time, forgot code output method. second time, had correct output format. third time, produced output.
02,-"sangeetha-may 02, 2013 , -in-09:48:06:61 , -out-08:08:19:27 01,-"lohith-may 01, 2013 , missing in , -out-09:10:41:61 02,-"sushma-may 02, 2013 , -in-09:48:06:61 , -out-07:52:13:51 01,-"sushma-jan 01, 2013 , -in-09:07:06:50 , -out-05:39:01:63 03,-"lohith-may 03, 2013 , -in-11:39:44:08 , missing out
and here's code. study , learn.
import java.io.bufferedreader; import java.io.ioexception; import java.io.stringreader; import java.util.arraylist; import java.util.list; public class recordparser { private bufferedreader reader; private list<person> people; private list<string> output; public recordparser(bufferedreader reader) { this.reader = reader; this.people = new arraylist<person>(); this.output = new arraylist<string>(); } public void execute() throws ioexception { string line = null; while ((line = reader.readline()) != null) { string[] parts = line.split(" , "); addperson(new person(parts[0])); if ((parts[1].contains("-in-")) && (parts[1].contains("-out-"))) { string[] inout = parts[1].split("-out-"); person person = getperson(parts[0]); person.setintime(inout[0]); person.setouttime("-out-" + inout[1]); } else if (parts[1].contains("-in-")) { person person = getperson(parts[0]); person.setintime(parts[1]); } else { person person = getperson(parts[0]); person.setouttime(parts[1]); } } // output people string list (person p : people) { output.add(p.getperson()); } } private void addperson(person person) { (person p : people) { if (p.getnamedate().equals(person.getnamedate())) { return; } } people.add(person); } private person getperson(string namedate) { (person p : people) { if (p.getnamedate().equals(namedate)) { return p; } } return null; } public list<string> getoutput() { return output; } public static void main(string[] args) { string input = "02,-\"sangeetha-may 02, 2013 , -in-09:48:06:61\n" + "01,-\"lohith-may 01, 2013 , -out-09:10:41:61\n" + "02,-\"sushma-may 02, 2013 , -in-09:48:06:61\n" + "01,-\"sushma-jan 01, 2013 , -in-09:07:06:50-out-05:39:01:63\n" + "02,-\"sangeetha-may 02, 2013 , -out-08:08:19:27\n" + "02,-\"sushma-may 02, 2013 , -out-07:52:13:51\n" + "03,-\"lohith-may 03, 2013 , -in-11:39:44:08"; bufferedreader reader = new bufferedreader(new stringreader(input)); recordparser recordparser = new recordparser(reader); try { recordparser.execute(); (string s : recordparser.getoutput()) { system.out.println(s); } } catch (ioexception e) { e.printstacktrace(); } try { reader.close(); } catch (ioexception e) { e.printstacktrace(); } } public class person { private string namedate; private string intime; private string outtime; public person (string namedate) { this.namedate = namedate; this.intime = "missing in"; this.outtime = "missing out"; } public void setintime(string intime) { this.intime = intime; } public void setouttime(string outtime) { this.outtime = outtime; } public string getnamedate() { return namedate; } public string getperson() { stringbuilder builder = new stringbuilder(); builder.append(namedate); builder.append(" , "); builder.append(intime); builder.append(" , "); builder.append(outtime); return builder.tostring(); } } }
Comments
Post a Comment