1. Download several books as plain text (i.e., .txt files), which you will use t
ID: 3667163 • Letter: 1
Question
1. Download several books as plain text (i.e., .txt files), which you will use to test your application. A good place to find free books is Project Gutenberg.
Choose a variety books that have "Plain Text UTF-8" file downloads. Download them and save them in a convenient location as .txt files. You do not have to read the books.
Your application must work on any .txt documents of reasonable size, not just one or two specific books.
2.To your assign4 package created in Part 1, add a new class called TextAnalysis.
3. In your TextAnalysis class, construct an application that behaves as described below.
i. The user starts the application. The application displays a dialog box that askes the user to enter a word to search for in a book. (The user should enter a single word, but your application does not need to enforce this.) If the user cancels the dialog, the application quietly terminates. Otherwise, the user's input is converted to lowercase and is used as the target word later in the application.
ii. Next, the application displays a JFileChooser and allows the user to choose a file. (The user should choose some kind of text file, but your application does not need to enforce this.) If the user cancels the dialog, the application quietly terminates. Otherwise, the user's selection is saved as the file to be processed later in the application.
iii. The application opens the text file and reads it one word at a time. (A word is any sequence of symbols separated by whitespace. The next method of theScanner class correctly reads the next word.) Each word should be converted to lowercase as it is read. As it reads the words, the application collects the information needed to compute the following information.
-The number of times the user's target word occurs in the file.
-The average length of all the words in the file. (This will be a decimal number, not an integer.)
-The word that would come immediately before the user's target word if all the distinct words in the file were arranged in lexicographic order. (If there is no such word, return the user's word.) You must do this without actually putting the words into lexicographic order.
-The word that would come immediately after the user's target word if all the distinct words in the file were arranged in lexicographic order. (If there is no such word, return the user's word.) You must do this without actually putting the words into lexicographic order.
-Any other interesting statistic (of your choice) that involves both the target word and the words in the file.
The application reports the above statistics using a dialog message box.
Your application must adhere to the following guidelines.
-Your dialog boxes must be user friendly in all circumstances. Use full English sentences with correct grammar and punctuation. Break each line as appropriate. Make sure null strings are never displayed. (Your application should communicate clearly with the user.)
-Your application must not throw any exceptions under any circumstance. Exceptions are printed using red text in the console window. One common exception occurs when you attempt to use null strings or objects.
-Do not read input or print results to the console. You must use popup dialog boxes.
-You may assume the file the user selects contains at least one word.
4. Test your application thoroughly to make certain that it will work correctly for all plain text files.
Ensure that you have used meaningful variables names, consistent indentation/spacing, and appropriate comments. The class and all methods should be commented using Javadoc. Use additional comments, as needed. Overall, your program should have good programming style.
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class TextAnalysis
{
/**
* main calls a method containing all of the other methods.
* This program is a tool used to take a users input as a String and a target file.
* The program then finds the following information: number of words in file,
* number of times search word occurs in file, the average word length in file,
* the word alphabetically preceding search word in target file,
* the word alphabetically following search word in target file,
* number of words in target file not including search word.
* Displays information in a dialog box.
*/
public static void main (String[] args) throws FileNotFoundException
{
allMethods();
}
/**
* Calls an InputDialog box for the user to input a search word.
* If the search word is "null" or "", the program terminates.
* If the search word is NOT "null" or "", the fileFind method is called.
* Also Contains 8 String variables. These contain explanatory text plus whatever is returned by the methods being called.
* Creates a new Scanner for each method.
* Shows a MessageDialog box containing the Strings mentioned above.
*/
public static void allMethods () throws FileNotFoundException
{
final String word = JOptionPane.showInputDialog("Please enter a word to search for in a book!");
if (word == null || word.equals(""))
{
System.exit(1);
}
else
{
String searchWord = word.toLowerCase();
File foundFile = fileFind();
String a1 = "The search word is: " + searchWord;
String a2 = "The file being searched is: " + foundFile;
String a3 = "The number of words in the selected file is: " + wordCount(new Scanner(foundFile));
String a4 = "The number of times "" + searchWord + "" occurs in the selected file is: " + findWord(new Scanner(foundFile), searchWord);
String a5 = "The average word length in the selected file is: " + avgWord(new Scanner(foundFile));
String a6 = "The word alphabetically preceding the word being searched for is: " + before(new Scanner(foundFile), searchWord);
String a7 = "The word alphabetically following the word being searched for is: " + after(new Scanner(foundFile), searchWord);
String a8 = "The number of total words in excluding "" + searchWord + "" is: " + wordsExcludeSearchWord(new Scanner(foundFile), new Scanner (foundFile), searchWord);
JOptionPane.showMessageDialog(null, a1 + " " + a2 + " " + a3 + " " + a4 + " " + a5 +" " + a6 + " " + a7 + " " + a8);
}
}
/**
* Calls a JFileChooser in the C: directory.
* If the user selects a file this method returns the chosen target file.
* If the user hits cancel the program terminates.
*/
public static File fileFind ()
{
File g = new File("");
JFileChooser f = new JFileChooser("c:\");
int status = f.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
{
g = f.getSelectedFile();
}
else
{
System.exit(1);
}
return g;
}
/**
* Calculates the number of words in the target file as an integer.
* @param s Scanner created from the target file.
* @return number of words in target file as an integer.
*/
public static int wordCount (Scanner s)
{
int numWords = 0;
while (s.hasNext())
{
s.next();
numWords++;
}
return numWords;
}
/**
* Calculates the number of times the search word occurs in the target file.
* @param s Scanner of target file.
* @param a String containing users search word.
* @return Number of times users search word occurs in target file as an integer.
*/
public static int findWord (Scanner s, String a)
{
int count = 0;
String nextWord;
while (s.hasNext())
{
nextWord = s.next().toLowerCase();
if (a.equals(nextWord))
{
count++;
}
}
return count;
}
/**
* Calculates the average word length in the target filee.
* @param s Scanner of target file.
* @return average word length in target file as a double.
*/
public static double avgWord (Scanner s)
{
int count;
double wordLength = 0;
String nextWord;
for (count = 0; s.hasNext(); count++)
{
nextWord = s.next();
wordLength += nextWord.length();
}
double avg = (wordLength/count);
return avg;
}
/**
* Finds the word that alphabetically precedes the users search word.
* @param s Scanner of target file.
* @param a String containing users search word.
* @return word alphabetically preceding users search word.
*/
public static String before (Scanner s, String a)
{
String precWord = null;
String nextWord = null;
while (s.hasNext() && precWord == null)
{
nextWord = s.next().toLowerCase();
if (nextWord.compareToIgnoreCase(a) >= 0)
{
nextWord = null;
}
else if (nextWord.compareToIgnoreCase(a) < 0)
{
precWord = nextWord.toLowerCase();
}
}
while (s.hasNext())
{
nextWord = s.next().toLowerCase();
if (nextWord.compareToIgnoreCase(a) < 0 && nextWord.compareToIgnoreCase(precWord) > 0)
{
precWord = nextWord.toLowerCase();
}
}
return precWord;
}
/**
* Finds the word that alphabetically follows the users search word.
* @param s Scanner of target file.
* @param a String containing users search word.
* @return word alphabetically following users search word.
*/
public static String after (Scanner s, String a)
{
String afterWord = null;
String nextWord = null;
while (s.hasNext() && afterWord == null)
{
nextWord = s.next().toLowerCase();
if (nextWord.compareToIgnoreCase(a) <= 0)
{
nextWord = null;
}
else if (nextWord.compareToIgnoreCase(a) > 0)
{
afterWord = nextWord;
}
}
while (s.hasNext())
{
nextWord = s.next().toLowerCase();
if (nextWord.compareToIgnoreCase(a) > 0 && nextWord.compareTo(afterWord) < 0 )
{
afterWord = nextWord.toLowerCase();
}
}
return afterWord;
}
/**
* Finds number of words in users target file not including the users search word.
* @param s Scanner of target file.
* @param n New Scanner of target file.
* @param a String containing users search word.
* @return Number of words in users target file not including the users search word as an integer.
*/
public static int wordsExcludeSearchWord (Scanner s, Scanner n, String a)
{
int x = wordCount(s);
int y = findWord (n, a);
int z = x - y;
return z;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.