Can someone please fix my code for a hashtable frequencey co

Can someone please fix my code for a hashtable frequencey counter:

Implement a Java program that counts word frequencies in a text file. Use a hashtable to store the data the words are the keys, and their frequencies are the values. The output of the program is the complete list of all the words and their frequencies in descending order of frequencies when two words have the same frequency, output them by alphabetical order. Each line of output consists of a word, a tab, and the frequency. A sample input is \"The Tragedy of Hamlet, Prince of Denmark\", in the file Hamlet When processing the text, you should keep just the words, and discard all punctuation marks, digits, and so on. You also need to turn all upper case letters to lower cases.

Solution

Hi buddy, Please find the below java program. I\'ve added comments for your better understanding. I checked this program by reading the input from console. It\'s working fine :-)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.ArrayList;

/**
* Created by abdul on 2/6/2017.
*/
class WordCounter {
    public static String finalWord(String str) {
        //This method replaces all the apostraphes with empty string to add the character
        //after the apostraphe back to the string
        str = str.replaceAll(\"\'\",\"\");
        String processedWord = str.replaceAll(\"[^a-zA-Z]\", \" \").toLowerCase();
        return processedWord;
    }

private Hashtable<String, Integer> hash = new Hashtable<String, Integer>();

public void fileInput() throws FileNotFoundException {
    File text = new File(\"C:/Shake.txt\");
  
    String word;
    int count = 1;
    Scanner in = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<>();
    while (in.hasNextLine()) {
        String line = in.nextLine();
        line = finalWord(line);
        StringTokenizer st = new StringTokenizer(line);
        while (st.hasMoreTokens()) {
            word = st.nextToken();
            // if (hash.containsKey(word)) {
            //     hash.put(word, hash.get(word) + 1);
            //     //int count = (Integer) hash.get(word);
            //     //hash.put(word, count + 1);
            //     count++;
            // } else {
            //     hash.put(word, 1);
            //     count = 1;
            // }
            if(!hash.containsKey(word)){
                hash.put(word,0);
            }
            hash.put(word,1+hash.get(word));
        }
    }
    Map<String ,Integer> map = new TreeMap<String, Integer>(hash);
    //System.out.println(map);
    Set set = map.entrySet();
    Iterator i = set.iterator();
    while(i.hasNext()){
        Map.Entry me = (Map.Entry)i.next();
        //Adding all the elements in the map to an Array List \'list\'
        list.add((String)me.getKey());
    }
  
    //Sorting the Strings present in the list
    Collections.sort(list,new Comparator<String>(){
        @Override
        public int compare(String S1, String S2){
            //If count of 2 strings is equal, Then compare Strings
            if( map.get(S1)-map.get(S2) ==0){
                return S1.compareTo(S2);
            }
            //Compare strings based on their count in the map.
            return map.get(S2) - map.get(S1);
        }
    });
    for(String x : list) {
        System.out.println(x+\" \"+map.get(x));
    }
}

    public static void main (String[]args) throws FileNotFoundException {
        // WordCounter abc = new WordCounter();
        // abc.fileInput();
        new WordCounter().fileInput();

    }
}


INPUT : This is a sample text. Work\'s well with - also. Try this. This is sample. You can define your own sampleS

OUTPUT :

his 3
is 2
sample 2
a 1
also 1
can 1
define 1
own 1
samples 1
text 1
try 1
well 1
with 1
works 1
you 1
your 1

Can someone please fix my code for a hashtable frequencey counter: Implement a Java program that counts word frequencies in a text file. Use a hashtable to stor
Can someone please fix my code for a hashtable frequencey counter: Implement a Java program that counts word frequencies in a text file. Use a hashtable to stor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site