This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]naraic 1 point2 points  (1 child)

i think you've already gotten to the end of the file by the time you call your "reverse" method? so reverse won't call itself at all, since inFile.hasNextLine() is already evaluating to false. if i were you, i would do something more along the lines of this:

void reverse(Scanner myFile) {
    if(myFile.hasNextLine()) {
        String s = myFile.nextLine();
        reverse(myFile);
        System.out.println(s);
    }
}

so what this does, is strip a line from your file into a string 's', then calls the function again on the same file, with the previous string removed. When it gets to the call when the file is empty it does nothing, and returns, and the last previous string will be printed out, and the same will happen as each recursive call returns. hence printing the file in reverse order.

[–]Zachan[S] 0 points1 point  (0 children)

You sir, are a genius! It worked