// CSE 143, Winter 2011, Marty Stepp // This program demonstrates the ArrayList class. // It reads words from a file "words.txt" into an array list, then prints them, // then filters out all of the plural words and prints again. import java.io.*; import java.util.*; public class ProcessFile { public static void main(String[] args) throws FileNotFoundException { // String[] words = new String[5]; // int count = 0; ArrayList list = readFile("words.txt"); // System.out.println(list); // System.out.println("This file has " + list.size() + " words."); // list.add(2, "Jessica"); // System.out.println(list); // System.out.println("The first word is " + list.get(0)); System.out.println("'the' is found at index " + list.indexOf("the")); printBackwards(list); } public static ArrayList readFile(String fileName) throws FileNotFoundException { ArrayList list = new ArrayList(); Scanner input = new Scanner(new File(fileName)); while (input.hasNext()) { String word = input.next(); list.add(word); } return list; } public static void printBackwards(ArrayList list) { for (int i = list.size() - 1; i >= 0; i--) { String word = list.get(i); System.out.println(word); } } }