while loop - Java: read 2 files and compare line by line -
i'm trying compare 2 files line line. let's example:
file1
a,b,c,d,12 e,f,g,h,20 h,g,t,y,30
file2
f,g,h,j,30 e,h,j,f,50 a,b,c,d,60 e,f,g,h,70
i want output like:
a,b,c,d,12,50 e,f,g,h,20,70
i've written code:
while ((line1 = bufferedformattedreadermaster.readline()) != null) { temp1 = line1.substring(0, 7); temp3 = line1.substring(8, 10); while ((line2 = bufferedformattedreaderanalytical.readline()) != null) { temp2 = line2.substring(0, 7); temp4 = line2.substring(8, 10); if (temp1.equals(temp2)) { system.out.println("oh yes"); bufferedwriterfinalresults.write(temp1 + "," +temp3 + "," + temp4); bufferedwriterfinalresults.newline(); numberoffinalresults++; } } }
but output only:
a,b,c,d,12,50
and not next line.
the bufferedformattedreadermaster , bufferedformattedreaderanalytical can read 1 time. brush skills on i/o. did simple list based conversion of code. code may not compile have implement. there better ways well.
list<string> bufferedformattedreadermasterlist =new arraylist<string>(); list<string> bufferedformattedreaderanalyticallist =new arraylist<string>(); while ((line1 = bufferedformattedreadermaster.readline()) != null) { bufferedformattedreadermasterlist.add(line1 ); } while ((line2 = bufferedformattedreaderanalytical.readline()) != null) { bufferedformattedreaderanalyticallist.add(line2); } for(string line1:bufferedformattedreadermasterlist) { temp1 = line1.substring(0, 7); temp3 = line1.substring(8, 10); for(string line2:bufferedformattedreaderanalyticallist) { temp2 = line2.substring(0, 7); temp4 = line2.substring(8, 10); if (temp1.equals(temp2)) { system.out.println("oh yes"); bufferedwriterfinalresults.write(temp1 + "," +temp3 + "," + temp4); bufferedwriterfinalresults.newline(); numberoffinalresults++; } } }
Comments
Post a Comment