Write a program in java that reads a text file and counts the number of times ea
ID: 3763696 • Letter: W
Question
Write a program in java that reads a text file and counts the number of times each English word appears. Display the 25 most frequently used words. THE PROGRAM REQUIRES TWO CLASSES, the main program and a separate class called UseCount to hold a word and its count. The UseCount class needs two instance variables, one String to hold the English word and an int to hold the count of times this word appeared in the text. A constructor for UseCount should have one parameter to initialize the English word. The count should be initialized to one. The main program needs to sort an array of UseCount objects. To be sorted, the UseCount class must implement the Comparable interface and include a method compareTo. The class statement for UseCount should look like: public class UseCount implements Comparable { and the compareTo method in UseCount should be: /* For sorting with the Comparable interface */ public int compareTo( UseCount other ) { return other.count – this.count; } The main program should create an array that can hold 10,000 UseCount objects. After asking the user for the input filename, it should read one English word at a time from the file using the next() method of a Scanner object. For each word read, search the array of UseCount objects for an object that contains the word just read (ignoring case). If found, increment the count in that object. If not found, create a new object for that word and put it in the array. You will want to keep a count of the number of UseCount objects in the array. After all words have been read from the input file, you can sort the array of UseCount object in descending order by the count. There is a static method to sort an array of objects. java.util.Arrays.sort( use, 0, numWords ); where use is your array of UseCount objects and numWords is the number of objects in the array. The first 25 elements of the array will contain the 25 most commonly used words. Print the top 25 words and the number of times they appeared. When your main program uses the next() method of a Scanner object to read a word, it might read a word that has punctuation at the beginning or end. The following method will remove all non-letter characters from the beginning and end of a word. If there are no letters left after removing the punctuation, the method returns null. Your program will need to check if the return value is null. You will want to copy this method into your main program. Call this method right after you read each word in the data file. private static String cleanWord( String dirty ) { int numChar = dirty.length() - 1; // index of last character while (!Character.isLetter(dirty.charAt(numChar))) { // if ends in non-letter if (numChar == 0) { // if only one character return null; // nothing left } dirty = dirty.substring(0,numChar); // remove last character numChar--; // decrement last char index } while (!Character.isLetter(dirty.charAt(0))) { // if starts with non-letter dirty = dirty.substring(1); // remove first character } return dirty; }
CatInTheHat.txt
Example output (showing only the first 15 words)
Enter file name>catinthehat.txt
100 The
69 And
59 I
40 not
39 said
34 you
32 A
31 to
28 in
26 we
26 Cat
25 that
25 do
23 with
22 is
Explanation / Answer
Here is your solution :
1. MainClass.java
package chegg_split;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
UseCount[] useCount = new UseCount[10000];
Scanner in = new Scanner(System.in);
int length = 0;
System.out.println("Enter File Name");
String fileName = in.next();
in.close();
File file = new File(fileName);
try {
Scanner sc = new Scanner(file);
boolean flag = false;
while (sc.hasNext()) {
String rawWord = sc.next();
String word = cleanWord(rawWord);
for (int i = 0; i < length; i++)
{
if (useCount[i].word.equalsIgnoreCase(word))
{
useCount[i].count++;
flag = true;
}
}
if (!flag)
{
useCount[length] = new UseCount(word);
length++;
}
}
sc.close();
Arrays.sort(useCount,0,length);
for (int i = 0; i < 24; i++)
{
try
{
System.out.println(useCount[i].word + " " + useCount[i].count);
} catch (Exception e)
{
System.out.println("Total distinct words are less than 25 in file.");
break;
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static String cleanWord( String dirty ) {
int numChar = dirty.length() - 1; // index of last character
while (!Character.isLetter(dirty.charAt(numChar))) { // if ends in non-letter
if (numChar == 0) { // if only one character
return null; // nothing left
}
dirty = dirty.substring(0,numChar); // remove last character
numChar--; // decrement last char index
}
while (!Character.isLetter(dirty.charAt(0))) { // if starts with non-letter
dirty = dirty.substring(1); // remove first character
}
return dirty;
}
}
2. UseCount.java
package chegg_split;
public class UseCount implements Comparable<UseCount> {
public String word;
public int count;
public UseCount(String word) {
this.word = word;
this.count = 1;
}
@Override
public int compareTo(UseCount other) {
return other.count - this.count;
}
}
just copy these two codes in seperate files and run. thats it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.