The program below is fully working and reads two documents (words.txt and words2.txt). The words from both docs are stored in the tree map 'frequency data'.
Example of system output : 3 the
Which means 'the' appears three times in both docs combined.
I want to know how many times it appears in each text document.
I have tried to add another integer by doing: TreeMap<String, Integer, Integer>. So that I can give document but this does not work. Can I do this?
public static int getCount (String word, TreeMap<String, Integer> frequencyData) { if (frequencyData.containsKey(word)) { // The word has occurred before, so get its count from the map return frequencyData.get(word); // Auto-unboxed } else { // No occurrences of this word return 0; } }
public static void readWordFile(TreeMap<String, Integer> frequencyData) { Scanner wordFile; String word; // A word read from the file Integer count; // The number of occurrences of the word Integer doc; // The doc number from array of docs *** int x;
for (x=0; x<Docs.length; x++) //For loop to read the documents { //***
try { //wordFile = new Scanner(new FileReader("words.txt")); wordFile = new Scanner(new FileReader(Docs[x])); } catch (FileNotFoundException e) { System.err.println(e); return; }
while (wordFile.hasNext( )) { // Read the next word and get rid of the end-of-line marker if needed: word = wordFile.next( );
// Get the current count of this word, add one, and then store the new count: count = getCount(word, frequencyData) + 1; frequencyData.put(word, count); } } //****
}
// Array of documents static String Docs [] = {"words.txt","words2.txt"}; //Docs are stored here