Print every line of file, last to first, with recursion Had a discussion with a friend to solve this extra credit problem, and we came up with two separate solutions. What are the differences between these two? Is one better than the other? How can I make this code better?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;

public class ExtraCredit {

    public static void main(String[] args) throws FileNotFoundException {
        int count = 0;
        Scanner scan = new Scanner(new File("src/ExtraCredit.java"));
        recursiveCountEveryOtherLine(count, scan);
        scan = new Scanner(new File("src/ExtraCredit.java"));
        System.out.println("***SECOND METHOD***");
        recursiveScannerExploit(scan);
    }
    // Write a method called from main that prints
    // every other line of this file from last to first
    // use recursion NO LOOPS!!
    // print line 10, 8, 6, 4, 2

    public static void recursiveCountEveryOtherLine(int count, Scanner scan) {
        count++;
        if (!scan.hasNextLine()) {
            scan.close();
            return;
        } else if (count % 2 == 0) {
            String temp = scan.nextLine();
            recursiveCountEveryOtherLine(count, scan);
            System.out.println(count + " " + temp);

        } else {
            scan.nextLine();
            recursiveCountEveryOtherLine(count, scan);
        }
    }

    public static void recursiveScannerExploit(Scanner scan) {
        if (closeScanner(scan)) {
            return;
        }
        String tempGarbage = scan.nextLine(); // Used to pop an element off the scanner
        if (closeScanner(scan)) {
            return;
        }
        String temp = scan.nextLine();
        recursiveScannerExploit(scan);
        System.out.println(temp);
    }

    public static boolean closeScanner(Scanner scan) {
        if (!scan.hasNextLine()) {
            scan.close();
            return true;
        } else {
            return false;
        }
    }
}
As I get started I just want to mention that you have two unused imports: 

import java.io.FileInputStream;
import java.io.InputStream;


Now I'll address your questions: 


  What are the differences between these two? 


Virtually nothing other than the obvious: 

Method #1 depends on a counter parameter to decide whether or not the line should be printed whereas the method #2 does not. This makes method #1 a little awkward to read, but anyone who understands recursion (which is any good programmer) should catch on to what you're doing. Having the counter could use slightly more memory but it's unlikely something this small would have any impact on overall performance and it's generally not worthwhile to optimize something like this unless you have measured your program's performance and determined there is a trouble spot.

Method #2 depends on calling the closeScanner method twice, and invoking a method may carry a bit of overhead cost. However you really don't need to concern yourself with this unless you have measured the performance of your code and find that there is a problem. (See the answer to this question)


  Is one better than the other? 


Again, the two are so similar there isn't really an objective way to say with certainty which is "better". If I had to select one I would probably prefer method #2 as I think it would more straightforward for someone else to use. It doesn't require knowledge of recursion or passing a counter initialized to the correct value.


  How can I make this code better?


In general comment your code so that others recognize its intended purpose and expected inputs and outputs. Javadoc is a good way to do this. Something else you can do is modify your method(s) to return something (e.g. String) rather than printing to System.out so that you can write automated tests against them more easily.

Method #1: 

else if (count % 2 == 0) { does not need to have the else since you would have returned from the method if there is no next line.

When you call this method you don't need a counter reference in main, so instead just directly pass the value of count like this: 

recursiveCountEveryOtherLine(0, scan);

Method #2: 

Replace this line: String tempGarbage = scan.nextLine();
with this: scan.nextLine(); as you have no reason to keep a reference to the lines you aren't going to print.

Inside of the closeScanner method you have: 

else {
    return false;
}


you can simply write return false without the else since you would have returned true earlier if there is no next line.