dimanche 28 janvier 2018

Using .equals in a conditional

I'm writing a rather simple BST that is taking its data from an input file. The input file contains 100+ String values. One of my jobs is to check for repeat values. In order to do this I wrote a recursion method that takes one of those words and checks it against the other words present while traversing through the BST.

This is the recursion code:

public int doubleWord(Word wordToCheck) {
    return doubleWordRec(wordToCheck, root);
}

int repeat = -1;

private int doubleWordRec(Word newWord, Word p) {

    if (p != null) {


        doubleWordRec(newWord, p.getLeftChild());
        if (newWord.getEnglishWord().equalsIgnoreCase(p.getEnglishWord())) {
            return repeat = 0;  
        }
        doubleWordRec(newWord, p.getRightChild());

    }
    return -1;
}

This is the application of the recursion:

while (input.hasNext()) {
            String englishWord = input.next();
            String italianWord = input.next();
            passedWord = new Word(englishWord, italianWord);
            if (doubleWord(passedWord) != 0)
                addWord(passedWord);
        }

As you can see, the idea is to verify that the word that is going to be added to the BST through the addWord method will only do so if the recursion method doubleWord does not return 0. The issue I am running into is that it seems as if though the recursion doesn't do anything. It still prints all of the words regardless.

Aucun commentaire:

Enregistrer un commentaire