Write a Java program that will repeatedly perform the following text file analys
ID: 3767505 • Letter: W
Question
Write a Java program that will repeatedly perform the following text file analysis tasks.
It will count the number of words in the open text file and display this count along with the average word length (average number of characters per word), the total number of word characters, the total number of punctuation characters, the length of the shortest word, and the length of the longest word.
It will list all of the shortest words found in the text file.
It will list all of the longest words found in the text file.
It will search for a word specified by the user and report how many times the word occurs in the text file.
The program will display all of the results on the screen as well as writing the results to the output text file. The main method of your program should first ask the user for the name of the output file and open the file for writing. Then it will prompt user to enter a input text file name to read from. It should repeatedly offer the user a menu of the above tasks. Each of the above tasks should be implemented in your program as separate methods.
Note: A word is defined to be a string of symbols that is preceded and followed by whitespace or the beginning of the line or the end of the line, or a portion of such a string. If such a string begins and ends with non-punctuation characters, then the string is a word. If such a string begins or ends with punctuation characters, the word embedded in the string will be the string with preceding and ending punctuation characters removed. In either case, punctuation characters in the middle of the string should not be removed and should be considered part of the word. So, the punctuation count should include those punctuation characters before or after a word, but not include punctuation characters embedded in a word.
Notes: 1).The length of a word followed by a punctuation character does not include the punctuation character.
2). You should write a private method to handle the opening of the input text file, since you will need to open the input text file multiple times when performing the above tasks.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Long_Sort_word {
static int previousLongLine = 0;
static int previousShortLine = 10000;
public void printLongLine(HashMap longLineMap) {
Set keyofSet = longLineMap.keySet();
Iterator itr = keyofSet.iterator();
while (itr.hasNext()) {
Integer keys = (Integer) itr.next();
String value = (String) longLineMap.get(keys);
System.out.println("Line Number of Longest line: " + keys
+ " Longest line: " + value);
}
}
public void printShortLine(HashMap shortLineMap) {
Set keyofSet = shortLineMap.keySet();
Iterator itr = keyofSet.iterator();
while (itr.hasNext()) {
Integer keys = (Integer) itr.next();
String value = (String) shortLineMap.get(keys);
System.out.println("Line Number of Shortest line: " + keys
+ " Shortest line: " + value);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "d:\test.txt";
// This will reference one line at a time
String line = null;
int key = 0;
int lineSize = 0, lineNumber = 0;
Long_Sort_word ln = new Long_Sort_word();
HashMap longLineMap = new HashMap();
HashMap shortLineMap = new HashMap();
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
lineNumber++;
lineSize = line.length();
if (lineSize > previousLongLine) {
previousLongLine = lineSize;
longLineMap.clear();
longLineMap.put(lineNumber, line);
}
if (lineSize < previousShortLine) {
previousShortLine = lineSize;
shortLineMap.clear();
shortLineMap.put(lineNumber, line);
}
}
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
ln.printLongLine(longLineMap);
ln.printShortLine(shortLineMap);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.