0

I'm simply trying to find the position of an object in a Customer List, although it's constantly returning false when I use the equals() method.

I now understand that you have to create your own equals method to override the automatic one, yet I can't understand how to create one when I'm comparing a Customer inside a Customer array.

Here are my instance variables:

private Customer[] data;
private int size;
private final static int INITIAL_SIZE = 20;

Here is my method to find the position of the object:

public int findCustomerLocation(String name)
{
int spot = -1;
Customer cus = new Customer(name);
  for(int i = 0; i < size ; i++)
  {
    if((data[i].equals(cus)))
    {
      spot = i;
      System.out.println("spot is:"  + spot);
    }
    else
    {
      System.out.println("spot not found");
    }
  }
 return spot;
}//findCustomerLocation

(It's returning spot not found)

I'm trying to rewrite the equals method, but I'm a bit stuck, I'm trying to use if it's an instance of another, but it's still returning false

hello
  • 77
  • 1
  • 3
  • 12

4 Answers4

1

Your equals method just uses the same identity operator (==) as the default Object#equals(). Presumably you intend for Customer objects to be considered equal when some particular information inside them, such as an ID field, is equal. If so, you need to compare those fields inside your equals() (and you should always include every field involved in equals() in your hashCode()).

Something like this might be what you're looking for:

public boolean equals(Object other) {
    if(!other instanceof Customer) {
        return false;
    }

    Customer that = (Customer) other;
    return this.name.equals(that.name);
}
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • I included my `Customer` class, customers are just names, so I'm trying to see if a certain customer is already in the list, i.e. if their names are the same. I've never done `hashCode()` though. – hello May 23 '15 at 20:38
  • @hello As described in [the Javadoc](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--), you should always override `hashCode()` when you override `equals()`, or Bad Things can happen. In the case where you only have one field, such as this, you can just use its hash code: `return name.hashCode()`. – chrylis -cautiouslyoptimistic- May 23 '15 at 20:41
  • If this.name is null, it fails. You should use `java.util.Objects.equals(Object a, Object b)` –  May 23 '15 at 20:54
0

The equals method should implement the logic that dictates whether a given object is "equal to" the current (this) object. Logically it should not rely on fields or parameters that are not members of the class. In your case, you should not be using the data array or the size field in the equals method. Just add the check on the appropriate fields of Customer.

For example, assuming Customer has a field called name, then the following equals checks if two Customers are equal if they have the same name:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj == null) {
        return false;
    }

    if (getClass() != obj.getClass()) {
        return false;
    }

    Customer other = (Customer) obj;
    if (name == null) {
        if (other.name != null) {
            return false;
        }
    } else if (!name.equals(other.name)) {
        return false;
    }
    return true;
}

If you're using an IDE like Eclipse, there are shortcuts for automatically overriding both equals() and hashCode() methods in a consistent way. Usually, both methods should use the same class members so that if two objects are equal, their hashcode values are identical.

M A
  • 71,713
  • 13
  • 134
  • 174
0

You are comparing the Customer objects, not their names. Since they are not the same object, equals() will return false. To fix this add the following code to Customer:

public boolean equals(Object obj){
    if(obj instanceof Customer){
        Customer c = (Customer)obj;
        if(c.name.equals(this.name)
            return true;
    }
    return false;
}

EDIT: Sorry for the duplicate post, look at the post by chrylis, and insert obj != null before the instanceof statement

Good luck!

-1

Note that you have to implement hashCode when you implement equals. There are some things to consider to write these methods correctly. To be on the safe side it's best to let an IDE generate these methods. For example, in Eclipse, go Source > Generate hashCode() and equals()...

Community
  • 1
  • 1
ralfstx
  • 3,893
  • 2
  • 25
  • 41