Hello all, I'm having trouble writing a spell checker as my recursive function isn't returning what I would like it to. It is meant to chop off the ends of words likes "baker" to bake and check against a predined dictionary. I used recursion for words like "baker's". I have a function like this

public static Word findAllEndings(HashFunction1 hashTable, String word) { 
         Objects objs  = hashTable.findEndings(word);
         Word w = null;
         boolean isFound = false;
         String subword = null;
         if(objs == null) 
             return null; 
         { 
             int tries = 0;

             String[] endswith = objs.getEndswith();
             int pos = objs.getPos();


             if(endswith != null){ 
                 int l = endswith[tries].length();
                 subword = word.substring(0, word.length()-l);
                 w = hashTable.find(subword);
                 tries++;
                 }
             if(w != null) {
                 isFound = true;
                 return w;
             }
             if(w == null) { 
                 w =  findAllEndings(hashTable, subword); 
             }

             }
         return null; 
         } 

Here is the findEndings function in the hashtable.

public Objects findEndings(String word){
        pos = 0;
        String[] modifiers = {"'s", "s", "es", "ed", "d", "er", "r", "ly", "ing"};
        String[] endswith = new String[10];
        boolean hasRelevantEnding = false;
        for(String x: modifiers) { 
            if(word.endsWith(x) == true) { 
                endswith[pos] = x; 
                pos++; 
                hasRelevantEnding = true;
            }
        }
        if(hasRelevantEnding) {
            Objects objs = new Objects(endswith, pos); 
            return objs;
        }

        else
            return null;
    }

Which is the recursive part of the code. It is working with some words like excited. It is even working for a word like baker's. If I debug it the code is actually finding bake, but then it keeps iterating and tries over things that return null and apparently the last one returns null. So it comes out that it didn't find this even when it did. I think I'm missing something here :( If anyone could help it would be greatly appreciated.

Thanks!

Your findAllEndings method is puzzling. Your isFound variable is never used. Your tries variable is used only on line 16 where its value is always 0, meaning that you are ignoring all but the first element of endswith. You increment tries on line 19 even though it is never used again. You assign w a value on line 26 even though it is never used again.

What is the purpose of your recursive call to findAllEndings if the return value is thrown away?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.