Input file --> input.txt (contains the lines below)
---------------------------------------------
1. Something something
2. Something something
3. Something something
4. Something something
5. Something something
---------------------------------------------

Requirement:
Read the text file line by line and compare a character in the current line with all the lines below it.
For example if it reads line 2, then it should compare a character in line 2 with line 3, line 4 and line 5. If it reads line 3 then it should compare a character in line 3 with line 4 and line 5. And so on..

The current code reads a line, but then it compares a character in the current line with all the lines (above, below and including the current line)

How do I correct this?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class dfg {

public static void main(String[] args) throws Exception {
		
	File directory = new File("./in");
	File myfiles[] = directory.listFiles();
		
	for (File inputfile : myfiles) {
	  
		String filename = inputfile.getName();
		System.out.println("FileName = " + filename); 
			
		BufferedReader input1 = new BufferedReader(new FileReader(inputfile));
		String[] str1 = null;
		String[] str2 = null;
		String strLine1, strLine2;

		while ((strLine1 = input1.readLine()) != null){	
				
		str1=strLine1.split("");
		BufferedReader input2 = new BufferedReader(new FileReader(inputfile));
		
			while ((strLine2 = input2.readLine()) != null) {
					
			str2=strLine2.split("");
			if (str1[3].equals(str2[13]))
			{
				System.out.println("Match Found");
			}
			}
		}
	}
	System.out.println("=================End of Program=================");
	}
}

You could read each line of the file into an array. Since you would know that the current line is at an index less than the lines after it, you could use that knowledge to compare characters without a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.