Question
Lab #3 will combine two new topics: loops and reading a text file. For this lab you will be reading a relatively large dictionary of
Lab #3 will combine two new topics: loops and reading a text file. For this lab you will be reading a relatively large dictionary of words. Since each word is on its own line you will use BufferedReader to read the file line by line. Although you could read a file of lines with a Scanner and a call to .nextLine() (like Project#3). the BufferedReader using .readLine() is much faster than the Scanner's .nextLine() for large files.
I have written the while loop for you that reads in a large dictionary file of words. If you click this link it may take several seconds to download the dictionary file into your browser window. You will read this file line by line. Each word is on its own line. As you read each word you will examine it with String, char and arithmetic operations to gather the following statistics from the file:
As you would expect, the sum of items 4, 5 and 6 must equal the total number of words read from the file.
total number of words read from the input file.
length of the longest word in the file
length of the shortest word in the file
total number of words that contained any one of these vowels: a,e,i,o and u
total number of words that contained none of the above, but contained the vowel y
total number of words that contained no vowels at all. i.e. did not contain any of a,e,i,o,u or y
import java.io.*;
import java.util.*;
public class Lab3
{
public static void main (String args[]) throws Exception // i.e. the input file you put on cmd line is not in directory
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
if (args.length < 1 )
{
System.out.println(" usage: C:\\> java Lab3 dictionary.txt"); // i.e. C:\> java Lab3 dictionary.txt
System.exit(0);
}
BufferedReader infile = new BufferedReader( new FileReader(args[0]) ); // we read our text file line by line .readLine()
int numOfWordsReadIn=0; // each line read in has exactly one word on it.
int numOfWordsWithAnyOf_aeiou=0; // contains any of these: a,e,i,o and u
int numOfWordsWith_y_ButNoneOf_aeiou=0; // contains y but none of a,e,i,o or u
int numOfWordsWithNoVowels=0; // contains none of a, e, i, o, u, or y
int shortestWordLength=Integer.MAX_VALUE,longestWordLength=0;
while( infile.ready() ) // BufferedReader's .ready() means same as Scanner's .hasNextLine()
{
String word = infile.readLine();
++numOfWordsReadIn;
// ---------- DONT WRITE ANYTHING ABOVE THIS LINE -------------------
WRITE CODE HERE
// ----------- DONT WRITE ANYTHING BELOW THIS LINE -------------------
} // END WHILE FILE HAS ANOTHER LINE IN IT
infile.close();
// PRINT SUMMARY VARIABLES
System.out.println("total words read from file: " + numOfWordsReadIn );
System.out.println("longest word length: " + longestWordLength );
System.out.println("shortest word length: " + shortestWordLength );
System.out.format("%6d words contained any of: a e i o u ",numOfWordsWithAnyOf_aeiou );
System.out.format("%6d words contained 'y' but none of: a e i o u ", numOfWordsWith_y_ButNoneOf_aeiou);
System.out.format("%6d words contained no vowels (i.e. none of: a e i o u y) ", numOfWordsWithNoVowels );
} // END MAIN
} // END LAB3 CLASS
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started