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=================");
	}
}

Dani AI

Generated

The inner loop in the original approach always reopens the file and starts at the top, so each current line gets compared with itself and the lines above it. 's suggestion to read the file into an array (or List) is the simplest, clearest fix: load all lines, then use index-based loops where the inner loop starts at i+1 so only subsequent lines are compared.

A concise, robust pattern (Java 7+) is shown below. It checks lengths before calling charAt to avoid IndexOutOfBoundsException and uses zero-based positions for clarity.

import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

List<String> lines = Files.readAllLines(Paths.get("in/input.txt"), StandardCharsets.UTF_8);

int posA = 3;   // zero-based index in current line
int posB = 13;  // zero-based index in lines below

for (int i = 0; i < lines.size(); i++) {
    String a = lines.get(i);
    if (a.length() <= posA) continue;
    char ca = a.charAt(posA);

    for (int j = i + 1; j < lines.size(); j++) {
        String b = lines.get(j);
        if (b.length() <= posB) continue;
        if (ca == b.charAt(posB)) {
            System.out.println("Match: lines " + (i + 1) + " and " + (j + 1));
        }
    }
}

Notes and cautions: avoid using split("") to get characters (it produces awkward empty elements and is error-prone). If the file is too large to hold in memory, either stream with a second reader that skips the first N lines for each outer iteration (slow, O(n^2) I/O) or use an approach that stores only the needed characters (for example, a first pass that extracts the target character per line into a compact array) to reduce memory usage.

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.