// Helene Martin, CSE 143 // Reverses the order of tokens in a file and filters out plural words. import java.util.*; import java.io.*; public class ReverseFile { public static void main(String[] args) throws FileNotFoundException { ArrayList words = fileToArrayList("words.txt"); printReverse(words); for (int i = 0; i < words.size(); i++) { if (words.get(i).endsWith("s")) { //words.set(i, words.get(i).toUpperCase()); words.remove(i); i--; // since remove shifts values over, we have // to be careful to not skip values. Use jGRASP // debugger to see this at work. } } System.out.println(words); } // Reads a file named fileName and returns an ArrayList containing all the file's // tokens in their original order. public static ArrayList fileToArrayList(String filename) throws FileNotFoundException { Scanner input = new Scanner(new File(filename)); ArrayList words = new ArrayList(); while(input.hasNext()) { words.add(input.next()); } return words; } // Prints the Strings in words in reverse order, one per line. public static void printReverse(ArrayList words) { for (int i = 0; i < words.size(); i++) { System.out.println(words.get(words.size() - 1 - i)); } } }