OK this is a recursive method which accepts a String, breaks it down into all possible combinations of characters, and tests which possibilities are found in a Set of words representing a dictionary, which is stored as a field ("dictionary"). Here are the actual instructions for the method:
"In this method you should return a set containing all words from the dictionary that can be made using some or all of the letters in the given phrase, in alphabetical order. For example, if your anagram solver is using the dictionary corresponding to dict1.txt and you are passed the phrase "Barbara Bush", you should return a set containing the elements [abash, aura, bar, barb, brush, bus, hub, rub, shrub, sub].
I figure that each call should test whether the dictionary contains the given possibility. If it is, the possibility is added to a new Set called "anagrams".
note: "phrase" initially = ""
public Set getWords(String phrase, String chosen) {
...if (phrase == null) {
.......throw new IllegalArgumentException();
...}
...Set anagrams = new TreeSet();
......if (dictionary.contains(chosen)) {
.........anagrams.add(chosen);
.........anagrams.addAll(getWords(phrase, chosen));
......} else {
.........for (int i = 0; i < phrase.length(); i++) {
............String ch = phrase.substring(i, i + 1 );
............String temp = phrase.substring(0, i) + (i + 1);
............anagrams.addAll(getWords(temp, chosen + ch));
......}
....}
....return anagrams;
}
But when I try to run it, it gives me a "stack overflow error." I don't even know that that means exactly, but I suspect that I'm making a bad recursive call. Does anyone know what I might be doing wrong?