class readability extends basic
{ public static void main (String param[]) throws Exception
{
// establish keyboard and screen input and output
input keyboard = new input();
output screen = new output();
screen.writeln("Type file name");
// establish input and output text files
input in = new input(keyboard.readword());
output out = new output("results");
in.readblanks();
assume(in.more(), "empty text"); // "assume"
checks that the file was
// successfully opened and is nonempty
int sentences = 0, words = 0;
// Text: read a series of sentences until the file is empty
while(in.more())
{ String word = in.readname();
in.readblanks();
words = words + 1;
// Sentence: read a sentence
while ((in.next()!=':')
&& (in.next()!='?') && (in.next()!='.'))
// AnotherWord: read the next word
{ if (in.next()==',')
{ in.readnext(); in.readblanks(); }
word = in.readname();
in.readblanks();
words = words + 1;
}
in.readnext(); in.readblanks();
sentences = sentences
+ 1;
}
// Compute average sentence length and save results
int length = words / sentences;
if (length <= 8) out.write ("4th grade");
else if (length <= 11) out.write ("5th grade");
else if (length <= 14) out.write ("6th grade");
else if (length <= 17) out.write ("7th grade");
else if (length <= 24) out.write ("High school");
else out.write ("College");
out.writeln (" level: " + length + " words per
sentence");
in.close(); out.close();
keyboard.close(); screen.close();
}
}
Taking input from a text file is similar to taking input from the keyboard, except that the user doesn't need to type it every time the program is run. This program uses the same methods as the familiar ones that you have used for keyboard input (see summary on page 227). To take input from a text file, the program declares a new file variable as follows:
input in = new input("<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 word = in.readname();
Now add this program to your project in place of the test.java file, and run it once for each of the input text files poem, tiny, and literacy.
class readability extends basic
{
public static void main (String param[]) throws Exception
{
input keyboard = new input();
output screen = new output();
int sentences = 0, words = 0;
// Establish input text file
screen.writeln("Type file name");
input in = new input(keyboard.readword());
in.readblanks();
// Text: read a series of sentences until the file is empty
while(in.more())
{ String word = in.readname();
// first word of a sentence
in.readblanks();
words = words + Sentence(in,
screen); // number of words in a sentence
sentences = sentences
+ 1; // and one more sentence
in.readnext(); in.readblanks();
}
Results(words, sentences);
// save results in a file
}
// Sentence: read a sentence and return the number of words in it;
//
call AnotherWord for each word in the sentence
static int Sentence(input in, output screen) throws Exception
{
int count = 1;
return count;
}
// AnotherWord: read the next word and return it
static String AnotherWord(input in) throws Exception
{ String word = "";
return word;
}
// Results: compute average sentence length and save results
static void Results (int words, int sentences) throws Exception
{
output out = new output("results");
out.close();
}
}
You may work in teams of two on this project. If so, both team
members' names should appear on all work completed jointly.