Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in java that reads a text file and counts the number of times ea

ID: 3762525 • 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

Explanation / Answer

import java.io.*; import java.util.Scanner; public class CountTheNumberOfAs { public static void main(String[] args)throws IOException { String fileName = "JavaIntro.txt"; String line = ""; Scanner scanner = new Scanner(new FileReader(fileName)); try { while ( scanner.hasNextLine() ){ line = scanner.nextLine(); int counter = 0; for( int i=0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote