import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; /** * Hold lines from Shakespeare plays * @author gtowell * Created: Sep 28, 2020 */ public class Shak { // Place where all the lines live ArrayList lines ; /** * The number of lines * @return */ public int size() { return lines.size(); } /** * Return the nth line in the store * @param no the line number * @return a line object if the line is good */ public Line getLineNo(int no) { if (no>0 && no < lines.size()) return lines.get(no); return null; } /** * The first line containing the given string * @param lookFor the string to be found * @return a Line object containing the first line found to contain the string */ public Line firstLineContaining(String lookFor) { for (Line l : lines) { if (l.getText().indexOf(lookFor) >= 0) return l; } return null; } /** * Create the structure and fill it from the given file * @param filename */ public Shak(String filename) { super(); lines = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader(filename));) { while (br.ready()) { try { Line l = new Line(br.readLine()); lines.add(l); } catch (ParseException pe) { System.err.println("Parse problem: " + pe); } catch (NumberFormatException ne) { System.err.println(ne); } } } catch (FileNotFoundException fof) { System.err.println(fof); } catch (IOException ioe) { System.err.println(ioe); } } }