// Helene Martin, CSE 143 // Reverses the order of tokens in a file. Attempts to filter out plural words. // goal: demonstrate ArrayList usage and challenges. import java.util.*; import java.io.*; public class ReverseFile { public static void main(String[] args) throws FileNotFoundException { ArrayList words = readFile("words.txt"); // attempt to remove all words that end with 's' // capitalizing them worked well but remove doesn't delete all of them! // use the jGRASP debugger to see why 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--; // one possible fix } } System.out.println(words); printReverse(words); } // Reads a file named fileName and returns an ArrayList containing all the file's // tokens in their original order. public static ArrayList readFile(String fileName) throws FileNotFoundException { Scanner input = new Scanner(new File(fileName)); ArrayList words = new ArrayList(); while (input.hasNext()) { String word = input.next(); words.add(word); } 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)); } } }