// Hunter Schafer, CSE 143 // This program reads in a file and prints out the lines in reverse order import java.io.*; import java.util.*; public class ReverseFile { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("words.txt")); // Hunter: First approach that didn't work // String[] lines = new String[1000]; // int count = 0; // while (input.hasNextLine()) { // String line = input.nextLine(); // lines[count] = line; // count++; // } // // lines: [Hunter, sells, ...] // for (int i = count - 1; i >= 0; i--) { // System.out.println(lines[i]); //} // Hunter: First construct an empty ArrayList ArrayList lines = new ArrayList(); while (input.hasNextLine()) { String line = input.nextLine(); lines.add(line); } // lines: [Hunter, sells, sea, ...] // Hunter: Note that we now says lines.size() to get the size (see note at bottom) for (int i = lines.size() - 1; i >= 0; i--) { // Hunter: Note that we say lines.get(i) instead of lines[i] System.out.println(lines.get(i)); } // Hunter: Java annoningly did not use the same naming convention across classes // String: .length() // Array: .length // ArrayList: .size() } }