0

I'm running into a problem while using a Trie in Java for an anagram solver. The following code is for Android and does not give me any permutation results for the letters that are given and passed in. However, if I implement the same code in a Java desktop application, it returns the correct results for me. I'm pretty sure it's something simple that's going wrong, I just can't seem to find it.

Code for Desktop version:

static String myWord;
    static String myLetters = "";
    static char[] myChars;
    static TrieNode myNode = new TrieNode();
    static TrieNode currentNode;
    static ArrayList<String> availableWords = new ArrayList<String>();

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        myLetters = s.next();

        readWords();
        getPermutations();
    }
    public static void getPermutations(){
        currentNode = myNode;
        for(int x = 0; x < myLetters.length(); x++){
            if(currentNode.children[myLetters.charAt(x) - 'a'] != null){
                availableWords.addAll(currentNode.getWords());
                currentNode = currentNode.children[myLetters.charAt(x) - 'a'];
                System.out.println("x: " + x + " " + currentNode.getWords() + "" + myLetters.charAt(x));
            }
        }   
        System.out.println(availableWords);
    }

Desktop Output:
aelpp  //(user input from scanner)
x: 0 []a
x: 1 [ae]e
x: 2 [ale, lea]l
x: 3 [leap, pale, peal, plea]p
x: 4 [appel, apple, pepla]p
[ae, ale, lea, leap, pale, peal, plea] 

Android code:

static String myWord;
    static char[] myChars;
    static TrieNode myNode = new TrieNode();
    static TrieNode currentNode;
    static ArrayList<String> availableWords = new ArrayList<String>();

    public static void getPermutations(String passedInLetters){
        currentNode = myNode;
        Log.i("TRIE:" , passedInLetters);
        for(int x = 0; x < passedInLetters.length(); x++){
            if(currentNode.children[passedInLetters.charAt(x) - 'a'] != null){
                availableWords.addAll(currentNode.getWords());
                Log.i("TRIE:" , "x: " + x + " " + currentNode.getWords());
                currentNode = currentNode.children[passedInLetters.charAt(x) - 'a'];
            }
        }
        Log.i("TRIE:", availableWords.toString());
    }

Android Log Output:

03-15 22:45:11.670: I/TRIE:(4286): aelpp
03-15 22:45:11.670: I/TRIE:(4286): x: 0 []
03-15 22:45:11.670: I/TRIE:(4286): x: 1 []
03-15 22:45:11.670: I/TRIE:(4286): x: 2 []
03-15 22:45:11.670: I/TRIE:(4286): x: 3 []
03-15 22:45:11.670: I/TRIE:(4286): x: 4 []
03-15 22:45:11.670: I/TRIE:(4286): []

If needed, here are the full classes:

Desktop: http://pastie.org/3604515 Android: http://pastie.org/3604513

I have been studying this post among some of my other sources: java string permutations and combinations lookup

Thanks!

EDIT:

These lines needed switched inside the if statement:

currentNode = currentNode.children[passedInLetters.charAt(x) - 'a'];
                availableWords.addAll(currentNode.getWords());
Community
  • 1
  • 1
Jordan
  • 609
  • 1
  • 12
  • 29
  • 2
    Vote to close: Asking strangers to spot errors in your code by inspection is not productive. You should identify (or at least isolate) the problem by using a debugger or print statements, and then come back with a more specific question (once you've narrowed it down to a 10-line [test-case](http://sscce.org)). – Oliver Charlesworth Mar 15 '12 at 23:07
  • Check your `readWords` method and the path that reads in the txt file. +1 for more Logs in your app. – zapl Mar 15 '12 at 23:26
  • fixed. I was looking at availableWords value before setting currentNode inside the if. switched the lines in the edit above and it worked perfectly. @Oli, I vote to close. – Jordan Mar 16 '12 at 02:01

0 Answers0