String comparison using .equals() does not work in Java.

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

3 Answers

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.

7

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.

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!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like