import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import java.util.Vector; public class Play { private final static String DELIMITERS = " .?!()[]{}|/&\\,;:-\'\"\t\n\r"; private Vector text; public Play( String filename ) { text = new Vector(); try { BufferedReader inputFile = new BufferedReader( new FileReader(filename) ); String buffer = inputFile.readLine(); while ( buffer != null ) { text.addElement( buffer ); buffer = inputFile.readLine(); } } catch ( IOException e ) { System.err.println( "Error opening reader on file: " + filename ); } } public int startsWith( char targetChar ) { targetChar = Character.toLowerCase( targetChar ); return countWords( new StartsWith(targetChar) ); } public int wordsOfLength( int targetLength ) { return countWords( new OfLength(targetLength) ); } public int numberOfPalindromes() { return countWords( new IsAPalindrome() ); } public int countWords( WordFeature test ) { int wordCount = 0; String line; StringTokenizer words; String word; for ( int i = 0; i < text.size(); i++ ) { line = text.elementAt(i).toLowerCase(); words = new StringTokenizer( line, DELIMITERS ); while( words.hasMoreElements() ) { word = (String) words.nextElement(); if ( test.hasFeature( word ) ) wordCount++; } } return wordCount; } }