I got 99% done just need help with sorting code.
It reads the text file that i added in the main class.
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Scanner; import java.io.File; public class WordCounter { private HashMap<String, Integer> wc; public WordCounter(String filename) { wc = new HashMap<String, Integer>(); countWords(filename); } private void countWords(String filename) { try { Scanner scan = new Scanner(new File(filename)); while (scan.hasNextLine()) { String[] words = scan.nextLine().split("[^a-zA-Z]+"); // track of the frequency for (String s :words){ String word= s.toLowerCase(); if(wc.containsKey(word)){ int x= wc.get(word); x++; wc.put(word, x); } else wc.put(word, 1); } } } catch (Exception e) { System.err.println(e); System.out.println("Exc"); } } public HashMap<String, Integer> getWc() { return wc; } public void print() { System.out.println("Word:\t\t\tFrequency:"); System.out.println("-------------------------------------------------"); for (String key : wc.keySet()) System.out.println(key + "\t\t\t" + wc.get(key)); } public static void main(String[] args) throws Exception { String filename = "test-file.txt"; if (args.length >= 1) filename = args[0]; WordCounter wdc = new WordCounter(filename); wdc.print(); } }