title: Java StackOverFlowError - Bad Recursive Call?
body: <p>I'm given a set of strings called "dictionary", stored as a field, which represents a dictionary of words.</p>
  
  <p>I am to write a method which accepts a String parameter ("phrase") and returns a set containing all the words in the dictionary set that can be made from rearranging the characters in the given phrase. Basically I'm searching the dictionary for anagrams.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public Set&lt;String&gt; getWords(String phrase) {
      Set&lt;String&gt; anagrams = new TreeSet&lt;String&gt;();
      String chosen = "";
      anagrams.addAll(getWords(phrase, chosen));
      return anagrams;
  }
  
  public Set&lt;String&gt; getWords(String phrase, String chosen) {
      if (phrase == null) {
          throw new IllegalArgumentException();
      } 
      Set&lt;String&gt; anagrams = new TreeSet&lt;String&gt;();
      if (dictionary.contains(chosen)) {
          anagrams.add(chosen);
          anagrams.addAll(getWords(phrase, chosen));
      } else {
          for (int i = 0; i &lt; phrase.length(); i++) {
              String ch = phrase.substring(i, i + 1);         
              String temp = phrase.substring(0, i) + phrase.substring(i + 1);
              anagrams.addAll(getWords(temp, chosen + ch));
          }
      }
      return anagrams;
  }    
  </code></pre>
  
  <p>So my approach is to:
  1. Check the dictionary for the possibility passed in this thread, represented by the variable "chosen." </p>
  
  <ol>
  <li>If the dictionary does contain the possibility, add it to the set called "anagrams" which is returned at the end of the call. Then pass the possibility again to try to make other combinations out of it.</li>
  </ol>
  
  <p>3.. If the dictionary does not contain the possibility, modify the string to try other possibilities, which are then tested recursively.</p>
  
  <p>The code above produces a "stack overflow error", which my research indicates means I'm doing infinite recursion, or passing the same string over and over again infinitely. I can't see where I'm doing this, though. Can you?</p>