Question

Below is the code that I wrote, bucketsRow contains 80 elements and the println statement inside for loop correctly shows that. readFile is a scanner which iterates through a file having 800 rows in which the elements are the first word, of which any 80 are present in bucketsRow. I wish to copy to those rows in the new file, for which I used filewriter.

for(int p=0;p<bucketsRow.length;p++)
{
    System.out.println("p:"+p+"Doc at p:"+names[p]);
    inner: while(readfile.hasNextLine())
    {
        String line=readfile.nextLine();
        //System.out.println(line);
        if(line.indexOf(names[p])!=-1)
        {
            System.out.println(line.indexOf(names[p]));
            writefile.write(line);
            writefile.write(System.getProperty( "line.separator" ));
            break inner;
        }
    }
}
writefile.flush();
writefile.close();     
Was it helpful?

Solution

The problem here is you need to set the focus of the scanner class reference pointer again to the start of the file and start comparing again.Because of this some of the rows are getting missed in the output file.

For getting the focus to the start of the file create a scanner reference freshly when you are entering the "while" loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top