Hello forum,

I am stuck with a field comparison problem for two files. In one file I have first names and other pet names. Now I am trying to get results where first names are within a 20 word range of pet names(+-20 for checking on each side of word boundary of pet name). First name file has 5 columns and pet name file has 3 columns. All columns are tab separated. The third column of firstnamefile.txt has the beginning index of first names. The second column of petnamefile.txt has the beginning index of pet names. I am trying to compare these to get the word range mentioned above for the entire coulms of two files.

Here goes my code:

import java.io.*;

public class NameAssc {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader fn = new BufferedReader(new FileReader("firstnamefile.txt"));
        BufferedReader pn = new BufferedReader(new FileReader("petnamefile.txt"));
        String s1;
        String s2;
        while (((s1=fn.readLine())!=null))
            String arr1[] = s1.split("\t");
            int fstart = Integer.parseInt(arr1[2]);

                String arr2[] = s2.split("\t");
                int pstart = Integer.parseInt(arr2[1]);

the problem is that the code returns correct values in the first iteration, but from the second iteration it does not enter the inner while loop.(tested it with print statements). Any help is appreciated. Please let me know if you have any questions about the problem.

6 Years
Discussion Span
Last Post by jaysean

Here is what your code does:

1) Reads the first line from fn reader in the outer while loop. while ((s1 = fn.readLine()) != null) 2) Reads all lines from the pn reader in the inner while loop (during the first iteration of the outer while loop). while ((s2 = pn.readLine()) != null) 3) When the outer while loop starts its second iteration, the second line from fn is read, but when it comes to the inner loop, the pn.readLine() returns null because all its lines have been read in the first iteration of the outer while loop. The pn reader was not reset to the beginning of the file just because another iteration of the while loop was started.

Try reading all of the data in first and then analyze it.

Hope this helps.

Edited by sergb: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.