Start New Discussion within our Software Development Community

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.

This article has been dead for over six months. Start a new discussion instead.