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");
}
}
"Type file name: "
A String variable is any variable declared with the type String. The following are String variable declarations:
String f, line;
The value of a String variable can be any String constant, including the empty String "" (the one that has no characters between the quotes).
The String class has a number of methods that allow programs to manipulate String values in different ways. The discussion below illustrates how some of these methods are used by showing some simple examples.
String s = "Hello World!";
The length of s is 12, and the method length returns the length of any String. Thus, the expression s.length() returns 12 in this example.
The ith character of a String variable can be isolated by using the charAt method. String values can be viewed as arrays of characters in this sense, so the expression s.charAt(0) returns the first character 'H', s.charAt(1) returns the second character 'e', and so forth. [Note that single characters are enclosed in single quotes (') rather than double quotes ("), which enclose strings. Thus the character H is written as 'H', while the 1-character String H is written as "H".]
Two String values can be concatenated (joined together) to form a single String, either using the + sign or using the concat method, which is part of the String class. Either of the following statements leaves the variable u with the value "Hello World! Hello!".
u = s + " Hello!";
u = s.concat(" Hello!");
Two String values can be compared for equality or nonequality using the equals method. Equality for String values means that one is an identical copy of the other, letter for letter. So we might write
if (s.equals("Hello!")) ...
to test whether or not the current value of s is identical to the String "Hello!". A related method is called equalsIgnoreCase , which will be true if the two strings are identical except for capitalization differences. For instance, if the value of s is "Hello", then s.equals("heLLO") is false but s.equalsIgnoreCase("heLLO") is true.
To test whether one string precedes another in an alphabetical sense, the compareTo method can be used. The expression s.compareTo("hello") returns a number < 0, 0, or > 0 depending on whether the value of s precedes, matches, or follows the string "Hello" alphabetically. So, considering an array 'words' of strings, the following if statement would test to see if string words[i] follows words[j] in the dictionary, and if so swap them:
if (words[i].compareTo(words[j]) > 0))
swap(words, i, j);
This example assumes, of course, that we have a method swap that will exchange the values of two entries in an array of strings.
t = s.substring(6, 11);
Here, the 6 designates the starting position of the substring to be copied, and the 11 designates the position of the next character in s following the substring (counting the first character at position 0, not 1). Note that the value of s itself is not affected by this action.
If we wanted to delete that same substring from s, we would need to concatenate the beginning part of s with that part which follows that substring. That is, we would write
s = s.substring(0, 5) + s.substring(11);
to obtain the string "Hello" and assign it to s. Note here that the expression s.substring(11) takes that substring which begins at position 11 and ends at the end of s. That is, it is the entire righthand end of s.
Finally, to insert a new substring within an existing String, we use concatenation and substring in a different way. For instance, to insert the word "Cruel" inside the string s = "Hello World!" to form s = "Hello Cruel World!" we would write the following assignment:
s = s.substring(0, 5) + " Cruel " + s.substring(6);
i = s.indexOf("Cruel");
s = s.replace(s.charAt(i), 'c');
The resulting value of i in this example is 6 (the position of the first character of the leftmost occurrence of the substring "Cruel" within s). The resulting value of s is "Hello cruel World!" since s.charAt(i) returns the character 'C'. Here are some simple exercises to check your understanding of the above ideas.
To access a file, the program should first declare a new variable for the file as follows:
BufferedReader in = new BufferedReader(new FileReader("<filename> "));
Here, <filename> denotes the name of the file which will supply the text input. Then whenever it needs to read, say, the next word in the text file and save it in the String variable word , it says:
String line = in.readLine();
When there are no more lines of text to be read from the file, the readString method will return the special constant null. This condition can therefore be tested directly, using the expression line==null .
To separate individual words from a line of text, a new StringTokenizer variable should be declared and initialized with the line of text.
StringTokenizer t = new StringTokenizer(line);Now, a "token" is just a series of nonblank characters in a string that is separated from the next token by one or more blanks. So a token is almost the same idea as a word. The special methods hasMoreTokens and nextToken are used to test if line has one or more tokens remaining, and to extract the leftmost remaining token from the string. For instance, the expression line.hasMoreTokens() tests the former situation and the statement w = line.nextToken() accomplishes the second.
Returning to the readability program shown above, exercise this program to see how it works. That is, add it to your project and then run it once for each of the input text files poem, tiny, and literacy.
abba dabba dabba dabba
dabba dabba dabba
said the monkey
to the
chimp.
A vocabulary list for this text has one occurrence of every different word that appears there:
abba
dabba
said
the
monkey
to
chimp
To solve this problem requires the use of a text file for input and an array of Strings to assist the computation of a word list. .