import java.util.*; import java.io.*; /* * Connor Moore * June 20, 2016 * * Parses a file, and reprints all lines in the file. */ public class FileParser { // Notes: // translation from array to ArrayList: // String[] => ArrayList // new String[10] => new ArrayList() // a.length => list.size() // a[i] => list.get(i) // a[i] = value; => list.set(i, value); // new operations: // list.remove(i); --remove the ith value // list.add(value); --appends a value // list.add(i, value); --adds at an index // list.clear() --remove all value // list.toString(); --nice String of the list public static void main(String[] args) throws FileNotFoundException { // array version of parsing a file String[] linesArray = new String[1000]; Scanner input = new Scanner(new File("data.txt")); int lineCount = 0; while (input.hasNextLine()) { String line = input.nextLine(); linesArray[lineCount] = line; lineCount++; } // this loop cannot use linesArray.length as the upper bound for (int i = 0; i < lineCount; i++) { System.out.println(linesArray[i]); } System.out.println(); // ArrayList version of parsing a file ArrayList linesList = new ArrayList(); // reset Scanner at the beginning of a data file input = new Scanner(new File("data.txt")); while (input.hasNextLine()) { linesList.add(input.nextLine()); } for (int i = 0; i < linesList.size(); i++) { System.out.println(linesList.get(i)); } System.out.println(); removePlurals(linesList); } // before the method: lines is an ArrayList containing lines of words // that will be processed. Cannot be null. // after the method: prints all lines without plural words. Plural words // are any word that ends with an "s". // Does not modify lines, I promise. public static void removePlurals(ArrayList lines) { for (int i = 0; i < lines.size(); i++) { Scanner line = new Scanner(lines.get(i)); while (line.hasNext()) { String word = line.next(); if (word.charAt(word.length() - 1) != 's') { System.out.print(word + " "); } } System.out.println(); } } }