I have two text files with the contents as below :
file1.txt
Hello
Second Line
Text line
Final Line
file2.txt
Final Line
Text line
line 3 of file2
World
What I am trying to do is use the each line from file2.txt as pattern to match against each line from file1.txt and print the line from file1.txt against which the pattern matches.
So far I have the following code :
BufferedReader file1= new BufferedReader(new FileReader("C:\\Users\\Ahmed Ismail Khalid\\Desktop\\file1.txt"));
BufferedReader file2= new BufferedReader(new FileReader("C:\\Users\\Ahmed Ismail Khalid\\Desktop\\file2.txt"));
while((line = inputfile.readLine()) != null)
{
while((pattern = patternsfile.readLine()) != null)
{
System.out.println(line);
Pattern r = Pattern.compile(pattern);
System.out.println(r);
Matcher m = r.matcher(line);
if (m.find( ))
{
System.out.println("Line corresponding to pattern in input.txt : " + line);
}
line = inputfile.readLine();
}
}
The output that I should get is :
Line corresponding to pattern in file1.txt : Text line 3
Line corresponding to pattern in file1.txt : Final Line
However the output I am getting is as below :
Hello
Final Line
Second Line
Text line
Text line
line 3 of file2
Final Line
World
Additionally, when I run the following code :
while((line = file1.readLine()) != null)
{
System.out.println(line);
while((pattern = file2.readLine()) != null)
{
System.out.println(pattern);
}
System.out.println();
}
The output I should get is
Hello
Final Line
Text line
line 3 of file2
World
Second Line
Final Line
Text line
line 3 of file2
World
Text Line
Final Line
Text line
line 3 of file2
World
Final Line
Final Line
Text line
line 3 of file2
World
However, the output I get is :
Hello
Final Line
Text line
line 3 of file2
World
Second Line
Text line
Final Line
It seems that when the first line from the file1.txt all the lines from the file2.txt are printed. However, after the second line from file1.txt is printed, the nested while loop does not execute and therefore, the lines from file2.txt are not printed.
I am just starting out with java and have absolutely no experience with it. Therefore any and all help would be appreciated.
Thanks
EDIT :
I know that the current code returns lines from file1.txt that contains a match from file2.txt somewhere in the line. However I how do modify the code such that I can find the closet string with difference of n letters. For example, suppose file1.txt has the lines :
Hello
Second Line
Text line
Final Line
and file2.txt has the following lines :
Final Linee
Text line
line 3 of file2
Helloo
And what I want is to return Final Line and Hello from file1.txt as they match Final Linee and Helloo from file2.txt.
And what I want is to return Final Line and Hello from file1.txt as they match Final Linee and Helloo from file2.txt.- Why notText lineas well? My answer precisely meets the requirements of your edited question. Just copy and execute it and let me know if you expect something different.System.out.println(matcher.group());withSystem.out.println(lineFromFile1);in my answer. I had writtenmatcher.group()for your original question. Feel free to comment in case of any issue/doubt.