java - How to find similar lines in two text files irrespective of the line number at which they occur -
i trying open 2 text files , find similar lines in them. code correctly reading lines both text files. have used nested for
loops compare line1
of first text file lines of second text file , on. however, detecting similar lines have same line number, (eg. line 1 of txt1
cc cc cc
, line 1 of txt2
cc cc cc
, correctly finds , prints it), doesn't detect same lines on different line numbers in files.
import java.io.*; import java.util.*; public class featureselection500 { public static void main(string[] args) throws filenotfoundexception, ioexception { // todo code application logic here file f1 = new file("e://implementation1/practise/comupdatusps.exe.hex-04-ngrams-freq.txt"); file f2 = new file("e://implementation1/practise/top-300features.txt"); scanner scan1 = new scanner(f1); scanner scan2 = new scanner(f2); int = 1; list<string> txtfileone = new arraylist<string>(); list<string> txtfiletwo = new arraylist<string>(); while (scan1.hasnext()) { txtfileone.add(scan1.nextline()); } while (scan2.hasnext()) { txtfiletwo.add(scan2.nextline()); } /* for(string ot : txtfiletwo ) { (string output : txtfileone) { // if (txtfiletwo.contains(output)) if(output.equals(ot)) { system.out.print(i + " "); system.out.println(output); i++; } } } */ (int j = 0; j < txtfiletwo.size(); j++) { string fsl = txtfiletwo.get(j); // system.out.println(filecontentsingleline); (int z = 0; z < 600; z++) // z < txtfileone.size() { string s = txtfileone.get(z); // system.out.println(fsl+"\t \t"+ s); if (fsl.equals(s)) { system.out.println(fsl + "\t \t" + s); // line // system.out.println(fsl); } else { continue; } } } } }
i made code nicer, you're welcome :) anyway, don't understand bug. runs through of list2 every line in list1...
import java.io.*; import java.util.*; public class featureselection500 { public static void main(string[] args) throws filenotfoundexception, ioexception { // todo code application logic here file file1 = new file("e://implementation1/practise/comupdatusps.exe.hex-04-ngrams-freq.txt"); file file2 = new file("e://implementation1/practise/top-300features.txt"); scanner scan1 = new scanner(file1); scanner scan2 = new scanner(file2); list<string> txtfile1 = new arraylist<string>(); list<string> txtfile2 = new arraylist<string>(); while (scan1.hasnext()) { txtfile1.add(scan1.nextline()); } while (scan2.hasnext()) { txtfile2.add(scan2.nextline()); } (int = 0; < txtfile2.size(); i++) { string linei = txtfile2.get(i); // system.out.println(filecontentsingleline); (int j = 0; j < txtfile1.size(); j++){ // z < txtfileone.size( string linej = txtfile1.get(j); // system.out.println(fsl+"\t \t"+ s); if (linei.equals(linej)) { system.out.println(linei + "\t \t" + linej); // line // system.out.println(fsl); } } } } }
Comments
Post a Comment