title: Finding out how many words appear in all 5 files
body: <p>I have this program that prints out as such:      </p>
  
  <pre><code>Max number files any word is in: 5  
  Total words in all files: 880  
  Total words in five files: 880  
  caesar.txt  
  hamlet.txt  
  likeit.txt  
  macbeth.txt  
  romeo.txt  
  
  List of words that appear in most files: [A, I, turn, O, after, a, time,, good, faithful, gone, etc...
  </code></pre>
  
  <p>I would like to know how many words there are that are in all 5 Shakespeare files (files used for testing can be found here: <a href="http://www.dukelearntoprogram.com/course3/archives/PracticeGladLibsData.zip" rel="nofollow">http://www.dukelearntoprogram.com/course3/archives/PracticeGladLibsData.zip</a>). I get a long list of words, which is not helpful at all for this problem.</p>
  
  <p>Here's my code in which this problem occurs:</p>
  
  <pre><code>import edu.duke.*;
  import java.util.*;
  import java.io.*;
  
  public class WordsInFiles {
      private HashMap&lt;String,ArrayList&lt;String&gt;&gt; wordInFilesMap;
  
      public WordsInFiles() {
          wordInFilesMap = new HashMap&lt;String,ArrayList&lt;String&gt;&gt;();
      }
      private void addWordsFromFile(File file) {
          //This method should add all the words from file into the map.
          //If a word is not in the map, then you must create a new ArrayList of
          // type String with this word, and have the word map to this ArrayList.
          //If a word is already in the map, then add the current filename to its
          // ArrayList, unless the filename is already in the ArrayList.
          FileResource fileResource = new FileResource(file);
          String fileName = file.getName();
          for (String word : fileResource.words()) {
              if (!wordInFilesMap.containsKey(word)) {
                  ArrayList&lt;String&gt; newList = new ArrayList&lt;String&gt;();
                  newList.add(fileName);
                  wordInFilesMap.put(word, newList);
              }
              else if (wordInFilesMap.containsKey(word) 
                          &amp;&amp; !wordInFilesMap.get(word).contains(fileName)) {
                  ArrayList&lt;String&gt; currentList = wordInFilesMap.get(word);
                  currentList.add(fileName);
                  wordInFilesMap.put(word,currentList);
              }
          }
      }
      private void buildWordFileMap() {
          wordInFilesMap.clear();
          DirectoryResource dirResource = new DirectoryResource();
          for (File file : dirResource.selectedFiles()) {
              addWordsFromFile(file);
          }
      }
      private int maxNumber() {
          //returns the maximum number of files any word appears in, considering
          // all words from a group of files.
          int highest = 0;
          for (String word : wordInFilesMap.keySet()) {
              ArrayList&lt;String&gt; currentFileList = wordInFilesMap.get(word);
              int currentNum = currentFileList.size();
              if (currentNum &gt; highest) {
                  highest = currentNum;
              }
          }
          return highest;
      }
      private ArrayList&lt;String&gt; wordsInNumFiles(int number) {
          //returns an ArrayList of words that appear in exactly number files
          ArrayList&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          for (String word : wordInFilesMap.keySet()) {
              ArrayList&lt;String&gt; currentList = wordInFilesMap.get(word);
              int currentFileCount = currentList.size();
              if (currentFileCount == number) {
                  wordList.add(word);
              }
          }
          return wordList;
      }
      private void printFilesIn(String word) {
          //prints the names of the files this word appears in, one filename per line
          ArrayList&lt;String&gt; fileNames = wordInFilesMap.get(word);
          for (int index=0; index &lt; fileNames.size(); index++) {
              System.out.println(fileNames.get(index));
  
          }
         }
      public void tester() {
          //call buildWordFileMap to select files and build HashMap of words
          buildWordFileMap();
          //determine maximum number of files any word is in, considering all words
          int fileNum = maxNumber();
          System.out.println("Max number files any word is in: "+fileNum);
  
          ArrayList&lt;String&gt; wordsInFiles = wordsInNumFiles(fileNum);
          System.out.println("Total words in all files: "+wordsInFiles.size());
          wordsInFiles = wordsInNumFiles(5);
          System.out.println("Total words in five files: "+wordsInFiles.size());
          printFilesIn("to");
          System.out.println("\n");
          printFilesIn("and");
          System.out.println("\nList of words that appear in most files: "+wordsInFiles);
  
          /*for (int index=0; index &lt; wordsInFiles.size(); index++) {
              System.out.println("Files where "+wordsInFiles.get(index)+" appear:");
              printFilesIn(wordsInFiles.get(index));
          }
  
          for (String key : wordInFilesMap.keySet()) {
              System.out.println("\nWord: "+key+"\tAppears in files: "
                                  +wordInFilesMap.get(key));
          }*/
  
      }
  } 
  </code></pre>