Write a program in java that reads a text file and counts the number of times ea
ID: 3762538 • 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class wordCount {
private static String cleanWord( String dirty ) {
int numChar = dirty.length() - 1;
while (!Character.isLetter(dirty.charAt(numChar))) {
if (numChar == 0) {
return null;
}
dirty = dirty.substring(0,numChar);
numChar--;
}
while (!Character.isLetter(dirty.charAt(0))) {
dirty = dirty.substring(1);
}
return dirty;
}
public static void main(String[]args) throws FileNotFoundException{
String fileName;
System.out.print("Enter the fileName>");
Scanner inputScanner = new Scanner(System.in);
fileName = inputScanner.nextLine(); // reading filename from user
inputScanner.close();
File text = new File(fileName); // opening file
//Creating Scanner instnace to read File
Scanner fileScanner = new Scanner(text);
List<UserCount> userCount = new ArrayList<UserCount>();
while(fileScanner.hasNextLine()){ // reading files
String line = fileScanner.nextLine();
String[] words = line.split(" ");
for(int i = 0;i < words.length;i++){
boolean flag = true;
for(int j = 0;j < userCount.size(); j++){
if (words[i].equals(userCount.get(j).word)){
userCount.get(j).count += 1; // updating count if word is present
flag = false;
}
}
if (flag){
UserCount obj = new UserCount(words[i]); // adding new word in array
userCount.add(obj);
}
}
}
fileScanner.close();
Collections.sort(userCount);
int min =userCount.size() > 25 ? 25: userCount.size(); // choosing atmost 25 result to display
for(int i = 0;i < min;i++){
System.out.println(userCount.get(i).count+" "+userCount.get(i).word);
}
}
}
class UserCount implements Comparable{
String word;
int count;
UserCount(String w){
this.word = w;
this.count = 1;
}
@Override
public int compareTo(Object arg0) { // Comparable function
// TODO Auto-generated method stub
UserCount us = (UserCount)arg0;
if(us.count > this.count){
return 1;
}else{
return -1;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.