algorithmicCoder
algorithmicCoder

Reputation: 6799

Why does this Recursive Java Program not correctly reverse lines from a text file

As a follow up to this question

Where the idea was to print lines from a file in reverse without using an explicit data structure I have an implementation question. Recursion was suggested and here's what I have:

import java.io.*;

public class ReverseLines {
   public ReverseLines() {
   }

   public void reverse(File fileToReverse, int n) {
      try {
         FileReader fr = new FileReader(fileToReverse);
         BufferedReader br = new BufferedReader(fr);
         String line = br.readLine();
         if (n > 0) {
            reverse(fileToReverse, n - 1);
         }
         System.out.println(line);
      } catch (Exception e) {
         System.out.println(e);
      }
   }

   public static void main(String[] argv) {
      ReverseLines testReverse = new ReverseLines();
      File test = new File("money.txt");
      testReverse.reverse(test, 3);
   }
}

This code prints the first line of money.txt 3 times instead of the first three lines in reverse. Frankly (and naively) I don't see how the recursion is supposed to work if just readLine(); is used.

Help is appreciated...

Thanks

Upvotes: 2

Views: 1062

Answers (3)

amit
amit

Reputation: 178491

you should use the same instance of BufferedReader rather then creating a new one in every run of the recursion.
also note that the System.out.println() should be inside the if condition (otherwise you print n+1 first lines)

should look something like that:

public class ReverseLines { 
    public void reverse(BufferedReader br, int n) {
        try {
            String line = br.readLine();
            if (n > 0) {
                reverse(br, n - 1);
                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] argv) throws FileNotFoundException {
        ReverseLines testReverse = new ReverseLines();
        File test = new File("money.txt");
        testReverse.reverse(new BufferedReader(new FileReader(test)), 3);
    }
}

Upvotes: 4

Petar Minchev
Petar Minchev

Reputation: 47393

You are always reading the first line of the file. Change it like this:

import java.io.*;

public class ReverseLines {
   public ReverseLines() {
   }

   public void reverse(BufferedReader br, int n) {
      try {            
         String line = br.readLine();
         if (n > 0) {
            reverse(br, n - 1);
            System.out.println(line);
         }             
      } catch (Exception e) {
         System.out.println(e);
      }
   }

   public static void main(String[] argv) {
      ReverseLines testReverse = new ReverseLines();
      File test = new File("money.txt");

      FileReader fr = new FileReader(test );
      BufferedReader br = new BufferedReader(fr); 

      testReverse.reverse(br, 3);
   }
}

Upvotes: 3

Zéychin
Zéychin

Reputation: 4205

You are re-initializing the buffered reader every time that you call the function, which makes it seek to the beginning of the file each time before you print anything out. Try it this way:

import java.io.*;

public class ReverseLines 
{
   private static BufferedReader br;
   public ReverseLines() 
   {

   }

   public void reverse(File fileToReverse, int n) {
      try {
         String line = br.readLine();
         if (n > 0) {
            reverse(fileToReverse, n - 1);
         }
         System.out.println(line);
      } catch (Exception e) {
         System.out.println(e);
      }
   }

   public static void main(String[] argv) {
      ReverseLines testReverse = new ReverseLines();
      File test = new File("money.txt");
      FileReader fr = new FileReader(fileToReverse);
      br = new BufferedReader(fr);
      testReverse.reverse(test, 3);
   }
}

Upvotes: 1

Related Questions