// Zorah Fung, CSE 143 // This program reads in a file and prints out the words in reverse order, // with each word on a new line. import java.util.*; import java.io.*; public class ReverseFile { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("words.txt")); /* Bad version with an array: String[] words = new String[1000]; int count = 0; while (input.hasNext()) { String word = input.next(); words[count] = word; count++; } for (int i = count - 1; i >= 0; i--) { System.out.println(words[i]); } */ // With an ArrayList (Ah! Much better) ArrayList words = new ArrayList(); while (input.hasNext()) { String word = input.next(); words.add(word); } for (int i = words.size() - 1; i >= 0; i--) { System.out.println(words.get(i)); } } }