INSTRUCTIONS
- Create a WordCounter class with a constructor that takes a file name as a parameter
- The class should have two fields: one for the file name and one for a HashMap to store word count information
- The constructor should call a private method, countWords, that reads in the file and counts the word frequencies
- The class should contain a get method for each field, as well as a print method that prints out the map in the following format: word: frequency
- When printing, the map should be sorted by either the word order or frequency (Hint: see Collections.sort)
- You should include the sample text file on Blackboard
import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; public class WordCounter { // 2 fields for file name and hashmap private ArrayList<String> fn,hm; private Scanner x; // constructor public WordCounter(String frame) { countwords(); } // methods private void countwords () { try{ x = new Scanner (new File("test.file.txt")); System.out.println(Collections.frequency(fn, hm)); } catch (Exception e){ System.out.println("File not found"); } } private void print() { } public static void main(String[] args) { String fname = "test-file.txt"; if (args.length > 0) fname = args[0]; WordCounter wc = new WordCounter(fname); wc.print(); } }
I still need help with step 4 and 5. please help im still learning