1

There's a LinkedQueueClass that has 3 elements:

"ABC" 
"XYZ" 
"123"

The elements inside the LinkedListQueue are of custom class type StringElement which has one data member, it holds a string (word data member).

StringElement: http://pastebin.com/zh0t2X5K

When I try to use the search() function to search for an element that actually exists in the queue the boolean value it returns is false. Means that it can't find the element.

Main: http://pastebin.com/vGegkcLZ

Where I went wrong?

EDIT (Code from Pastebin.org links mentioned above.)

Main:

public class StringElement extends DataElement {

    protected String word;

    public StringElement(String str)
    {
        word = str;
    }

    public StringElement(StringElement otherElement)
    {
        word = otherElement.word;
    }

    @Override
    public boolean equals(DataElement otherElement) {
        StringElement temp = (StringElement) otherElement;
        return (word == temp.word);
    }

    @Override
    public int compareTo(DataElement otherElement) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void makeCopy(DataElement otherElement) {
        StringElement temp = (StringElement) otherElement;
        word = temp.word;
    }

    @Override
    public DataElement getCopy() {
        StringElement temp = new StringElement(word);
        return temp;
    }

    @Override
    public String toString()
    {
        return String.valueOf(word);
    }

}

StringElement:

import java.util.Scanner;

public class Run {
    public static void main(String args[]){
        LinkedQueueClass strQueue = new LinkedQueueClass();    

        strQueue.addQueue(new StringElement("ABC"));
        strQueue.addQueue(new StringElement("XYZ"));
        strQueue.addQueue(new StringElement("123"));

        System.out.println();

        //ask user for a keyword to search for
        System.out.print("Search keyword: ");
        Scanner scan = new Scanner(System.in);
        String userInput;
        userInput = scan.next();

        //place the entered keyword into a StringElement and use it
        //to search for the element with mathing keyword
        StringElement keyword = new StringElement(userInput);
        System.out.println(keyword.toString());//debugging: to confirm userInput value got stored in keyword object
        System.out.println(strQueue.search(keyword));//search returns false


    }
}

search() from UnorderedLinkedList:

public boolean search(DataElement searchItem)
    {
        Node current; //pointer to traverse the list
            boolean found;

            current = first;  //set current pointing to the first
                                                  //node in the list

                found = false;    //set found to false

                while(current != null && !found)                //search the list
                        if(current.info.equals(searchItem)) //item is found
                        found = true;
                        else
                                current = current.link; //make current point to
                                                                        //the next node
                return found;
    }
Bob
  • 1,355
  • 5
  • 19
  • 38
  • The keyword object that you constructed is different from the search object. You are trying to search for a different object, though the contents are the same. – r0ast3d Nov 16 '11 at 20:24

2 Answers2

2

Your implementation of .equals uses == which will check for value equality, which is what you're looking for.

It seems that the .equals you are using in the search method is not the one you are expecting. Which is why you shouldn't mix the == and .equals functionality.

It's safer to implement your .equals in the consistent reference equality way, then use == to do value equality check in your search method.

In particular, this is the quirk you're experiencing with your .equals method. You're overriding something from object, not DataElement

Community
  • 1
  • 1
Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
0

Solution

Overriding DataElement's abstract equals() method like so:

public boolean equals(DataElement otherElement) {
        StringElement temp = (StringElement) otherElement;
        return (str.compareToIgnoreCase(temp.str) == 0);
    }
Bob
  • 1,355
  • 5
  • 19
  • 38