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

To test program, used WordCount.java, that calculates frequencies of words in te

ID: 3766726 • Letter: T

Question

To test program, used WordCount.java, that calculates frequencies of words in text.
It takes file name as command line argument. For example, I've created input.txt.
Expected output for it:
"The most frequent word is 'the' with 36 occurrences."

To run test program in console, use java WordCount input.txt

I tested the program. The output came out "Usage: WordCount file.txt"

I do not how to fix the program.

*************************************************

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class WordCount {

   public static void main(String[ ] args) {      
       if (args.length < 1){
           System.out.println("Usage: WordCount file.txt");
           return;
       }
      
       String input = args[0];
       File file = new File(input);
       Map<String,Integer> freq = new ChainHashMap<>( );
       try{
           // scan input for words, using all nonletters as delimiters
           Scanner doc = new Scanner(file).useDelimiter("[^a-zA-Z]+");
          
           while (doc.hasNext( )) {
               String word = doc.next( ).toLowerCase( ); // convert next word to lowercase
               Integer count = freq.get(word); // get the previous count for this word
  
               if (count == null)
                   count = 0; // if not in map, previous count is zero

               freq.put(word, 1 + count); // (re)assign new count for this word
           }
  
           int maxCount = 0;
           String maxWord = "no word";
  
           for (Entry<String,Integer> ent : freq.entrySet( )) // find max-count word
               if (ent.getValue( ) > maxCount) {
                   maxWord = ent.getKey( );
                   maxCount = ent.getValue( );
               }
  
           System.out.print("The most frequent word is '" + maxWord);
           System.out.println("' with " + maxCount + " occurrences.");
      
       }catch(IOException e){
           System.out.print("Unable to load words from file " + input);
       }

   }
}

Explanation / Answer

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class WordCount {
   public static void main(String[] args) {
       if (args.length < 1) {
           System.out.println("Usage: WordCount file.txt");
           return;
       }

       String input = args[0];
       // String input = "input.txt";
       File file = new File(input);
       Map<String, Integer> freq = new HashMap<String, Integer>();
       try {
           // scan input for words, using all nonletters as delimiters
           Scanner doc = new Scanner(file).useDelimiter("[^a-zA-Z]+");

           while (doc.hasNext()) {
               String word = doc.next().toLowerCase(); // convert next word to
                                                       // lowercase
               Integer count = freq.get(word); // get the previous count for
                                               // this word

               if (count == null)
                   count = 0; // if not in map, previous count is zero
               freq.put(word, 1 + count); // (re)assign new count for this word
           }

           int maxCount = 0;
           String maxWord = "no word";

           for (Entry<String, Integer> ent : freq.entrySet())
               // find max-count word
               if (ent.getValue() > maxCount) {
                   maxWord = ent.getKey();
                   maxCount = ent.getValue();
               }

           System.out.print("The most frequent word is '" + maxWord);
           System.out.println("' with " + maxCount + " occurrences.");

       } catch (IOException e) {
           System.out.print("Unable to load words from file " + input);
       }
   }
}

OUTPUT:

The most frequent word is 'hi' with 79 occurrences.

input.txt:

hi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli howhi hi sri palli how
hi hi sri palli how
hi hi sri palli how
hi hi sri palli how

Note:please set your jdk home path to Path environment variable

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