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 Your code is fine except for apostrophe was also being replacced by space because you were replaceing everything other than alphabets with space. I put apostrophe in exclusion list and then processed that seperately. Check the code below import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.ArrayList; /** * Created by abdul on 2/6/2017. */ public class WordCounter { public static String finalWord(String str) { String processedWord = str.replaceAll(\"[^a-zA-Z\']\", \" \").toLowerCase(); processedWord = processedWord.replaceAll(\"[\']\", \"\"); // replace \' with empty string return processedWord; } private Hashtable hash = new Hashtable(); public void fileInput() throws FileNotFoundException { File text = new File(\"E:/Shake.txt\"); String word; int count = 1; Scanner in = new Scanner(text); ArrayList 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; } } } Map map = new TreeMap(hash); //System.out.println(map); Set set = map.entrySet(); Iterator i = set.iterator(); while(i.hasNext()){ Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + \" \"); System.out.println(me.getValue()); } } public static void main (String[]args) throws FileNotFoundException { WordCounter abc = new WordCounter(); abc.fileInput(); } }.