JAVA CountOccurrenceOfWordsjava Your program should take a t

JAVA

CountOccurrenceOfWords.java

Your program should take a text file name as a command line argument. It should read the file and print the number of times the most-used word appears and, in ascending order, the words that occur most frequently. Ignore case when processing the words in the file. Your program may ignore punctuation. For example, if the file contained

The program\'s output should be

Words that appear 2 times: and are dachshunds dogs

Solution


//CountOccurrenceOfWords.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.*;

public class CountOccurrenceOfWords
{
    public static void main(String[] file) throws FileNotFoundException, IOException
    {
        if (file.length == 0)
        {
            System.out.println(\"Usage: java CountOccurrenceOfWords targetfile\");
            System.exit(0);
        }

        BufferedReader br = null;
        br = new BufferedReader(new FileReader(file[0]));
        String text = null;
        TreeMap<String, Integer> treemap = new TreeMap<String, Integer>();


        try
        {output
            while ((text = br.readLine()) != null)
            {
            String[] line = text.split(\"[ \ \\t\ .,;:!?(){}]\");

                for (int count = 0; count < line.length; count++)
                {
                    String key = line[count].toLowerCase();
                    if (key.length() > 0)
                    {
                        if (treemap.get(key) == null)
                        {
                            treemap.put(key, 1);
                        }
                        else
                        {
                            int value = treemap.get(key).intValue();
                            value++;
                            treemap.put(key, value);
                        }
                    }
                  
                }

            }
                Set<Map.Entry<String, Integer>> entrySet = treemap.entrySet();

                int max = 0;
                for (Map.Entry<String, Integer> entry : entrySet)
                    {
                        if(entry.getValue() > max) max = entry.getValue();
          
                    }
                // create an empty array list with an initial capacity
                ArrayList<String> maxFrequencyList = new ArrayList<String>(100);
                for (Map.Entry<String, Integer> entry : entrySet)
                    {
                        if(max == entry.getValue()) maxFrequencyList.add(entry.getKey());
          
                    }
                // sort maximum frequncy list
                Collections.sort(maxFrequencyList);

                System.out.print(\"Words that appear \" + max + \" times are: \");
                for (String word : maxFrequencyList)
                {
                       System.out.print(word + \", \");
                }

                System.out.println();
        }
        catch (IOException error)
        {
            System.out.println(\"Invalid File\");
        }
        finally
        {
            br.close();
        }
    }
}

/*
output:

Words that appear 2 times are: and, are, dachshunds, dogs,

*/

JAVA CountOccurrenceOfWords.java Your program should take a text file name as a command line argument. It should read the file and print the number of times the
JAVA CountOccurrenceOfWords.java Your program should take a text file name as a command line argument. It should read the file and print the number of times the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site