5

When comparing a string taken from console input to a string inside an array, it is always false unless I add .toString(). Both strings are equal and it should work without adding the .toString(). Can anyone help me figure out why?

Here I get the string I want to compare from the console:

System.out.println("\nEnter the name you wish to remove from the list.");
String name = in.nextLine();
System.out.println("\n\"" + myNameList.remove(new MyOrderedList(name)) + "\"" + " removed from the name list\n");

Here is the remove method:

public T remove(T element) {
    T result;
    int index = find(element);

    if (index == NOT_FOUND) {
        throw new ElementNotFoundException("list");
    }

    result = list[index];
    rear--;

    /** shift the appropriate elements */
    for (int scan = index; scan < rear; scan++) {
        list[scan] = list[scan+1];
    }

    list[rear] = null;
    return result;
}

Here is the find method that is were the problem is:

private int find(T target) {
    int scan = 0, result = NOT_FOUND;
    boolean found = false;

    if (!isEmpty()) {
        while (!found && scan < rear) {
            if (target.equals(list[scan])) { // Not sure why this does not work until I add the .toString()s
                found = true;
            }
            else {
                scan++;
            }
        }
    }

    if (found) {
        result = scan;
    }
    return result;
}

The if (target.equals(list[scan])) always returns false unless I change it to if (target.toString().equals(list[scan].toString()).

I am using an ArrayList to represent an array implementation of a list. The front of the list is kept at array index 0. This class is extended to create a specific kind of list if that helps. I can post all classes if needed.

3
  • 2
    Where is the definition of list? Commented Sep 17, 2012 at 13:27
  • I think it's due to generics - have a look at 'target' variable - is it really instance of String? Try to implement your own equals() and trace it... Commented Sep 17, 2012 at 13:31
  • 1
    Don't assume that because you can't use it, it's broken. If .equals() on String were broken, that would be pretty well known. Commented Sep 17, 2012 at 13:32

3 Answers 3

3

You are only using String.equals if the first argument is a String.

String comparison using .equals() does not work java

It appears this is the thing which does work. Its T.equals() which doesn't work.


If you have this working, it means you have overridden toString() sensibly.

target.toString().equals(list[scan].toString()

but if this doesn't work

target.equals(list[scan])

it means you haven't overridden equals(Object) correctly.

Sign up to request clarification or add additional context in comments.

7 Comments

you are doing injustice to us!! you answer all the ques before we could even think of answers
a most grievous injustice indeed :)
@Yadnesh ok, ok, I will give it break. ;)
I will try to wait until the question is 10 mins old.
@Yadnesh I have trouble telling the difference.
|
1

If myNameList has a String generic parameter, then this will not work, because no String will equal a type of MyOrderedList.

If myNameList has a MyOrderedList generic parameter, then you will need to make sure that you define an equals() method for it.

Comments

0

nicholas and peter are correct. i needed to override equals() method. I tried a few things and also let Eclipse generate the hashCode() and equals() to see what would happen and its working now without the .toString()

Here is what Eclipse generated for me:

    /* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((myList == null) ? 0 : myList.hashCode());
    return result;
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof MyOrderedList)) {
        return false;
    }
    MyOrderedList other = (MyOrderedList) obj;
    if (myList == null) {
        if (other.myList != null) {
            return false;
        }
    } else if (!myList.equals(other.myList)) {
        return false;
    }
    return true;
}

I really thank everyone for such quick responses. I am fairly new to java so i read a lot of posts here when i run into a problem and i always find the answer here. Thanks everyone!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.