import cs1.Keyboard; import java.io.*; import java.util.*; // Compute a readability index from a text stored in a file class readability { public static void main (String param[]) throws IOException { // Establish input text file System.out.println("Type file name"); String f = Keyboard.readString(); int sentences = 0, words = 0; // open the file and get the first line of text BufferedReader in = new BufferedReader(new FileReader(f)); String line = in.readLine(); // Read a series of lines of text while(line != null) { StringTokenizer t = new StringTokenizer(line); // Divide the line into individual words while (t.hasMoreTokens()) { String w = t.nextToken(); char c = w.charAt(w.length()-1); if (c=='.'|| c=='?' || c=='!') { sentences = sentences+1; w = w.substring(0,w.length()-1); } words = words + 1; } // read the next line of text line = in.readLine(); } // Compute average sentence length and display results System.out.print("Results: "); System.out.println( words + " words and " + sentences + " sentences."); int length = words / sentences; if (length <= 8) System.out.print ("4th grade"); else if (length <= 11) System.out.print ("5th grade"); else if (length <= 14) System.out.print ("6th grade"); else if (length <= 17) System.out.print ("7th grade"); else if (length <= 24) System.out.print ("High school"); else System.out.print ("College"); System.out.println (" level: " + length + " words per sentence"); in.close(); } }