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 lines are equal and they should work without adding .toString() . Can someone help me understand why?

Here I get the line that I want to compare with 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 removal 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 search method that was the problem:

 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; } 

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

I use ArrayList to represent an implementation of a list array. The front of the list is stored in the index of array 0. This class is extended to create a specific type of list, if that helps. I can publish all classes if necessary.

+4
source share
3 answers

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

If myNameList has a common parameter, MyOrderedList , you need to make sure that you define the equals() method for it.

+1
source

You only use String.equals if the first argument is String.

String comparison using .equals () does not work java

This seems to be what works. Its T.equals () is not working.


If this works for you, it means that you have significantly redefined toString() .

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

but if it does not work

 target.equals(list[scan]) 

this means that you did not redefine equals(Object) correctly.

+3
source

Nicholas and Petter are true. I needed to override the equals () method. I tried a few things and also let Eclipse generate hashCode () and equals () to see what happens and its work now without .toString ()

Here is what I created for Eclipse:

  /* (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 answers. I am 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!

0
source

Source: https://habr.com/ru/post/1434611/


All Articles