0

I'm trying to compare two objects of the same type (in method doesHave), but I've never got "true" returned. Here's my code:

private ArrayList<Osoba> osoba = new ArrayList<Osoba>();    

public boolean doesHave(Osoba o) {
        for (int i = 0; i < osoba.size(); i++) {
            if (pobierz(i).equals(o))
                return true;
        }
        return false;
    }

public Osoba pobierz(int index) {
            return osoba.get(index);
        }

The 'Osoba' class, looks like this:

public class Osoba {
    private String imie;
    private String nazwisko;

    public String toString() {
        return imie + " " + nazwisko;
    }

    public Osoba(String name, String surname) {
        imie = name;
        nazwisko = surname;
    }

    public String getImie() {
        return imie;
    }

    public String getNazwisko() {
        return nazwisko;
    }
}

Code from main:

Osoba osoby = new Osoba(name, surname);
if (kartoteka.doesHave(osoby) == Boolean.TRUE) {
            temp = odczyt.nextLine();
            if (temp.equals("y"))
                kartoteka.usun(osoby); //some method
            else
                ...
        }

No matter what input I'd use, this part never happens. 'kartoteka' is my package which of course I have imported. Each class is in separate package, but there is no problem with using them. I've tried googling it for quite some time, but nothing helped me, it just seems like

if (kartoteka.doesHave(osoby) == Boolean.TRUE)

Is never true, but I have no idea why. Code without Boolean.TRUE doesn't work as well. If I could get some hints, I'd be thankful. Sorry for asking that kind of question, but I'm new in java.

Dargenn
  • 372
  • 4
  • 11
  • 6
    `Osoba` class must implement `equals` method, and `hashCode` as well to follow best practices. – Luiggi Mendoza Mar 30 '15 at 15:36
  • 2
    Comparing a `boolean` value with `Boolean.TRUE` is asking for trouble. It will need to box the boolean return value into a `Boolean` instance and then will check if it is *the same instance* as `Boolean.TRUE`. – Marko Topolnik Mar 30 '15 at 15:39
  • 2
    possible duplicate of [Compare two objects with .equals() and == operator](http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Code Whisperer Mar 30 '15 at 15:40
  • 2
    Possible duplicate: http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator – Code Whisperer Mar 30 '15 at 15:40

4 Answers4

2

Right now your code checks if the memory address of two objects is the same which can only be true if it is the same exact object.

You need to implement a method that compares two Osoba objects by comparing whichever properties you want from these objects and returning true/false appropriately.

Code Whisperer
  • 1,041
  • 8
  • 16
2

Imagine that you and your wife have twins.

Are they the "same" person? No, they are not. Lets call them twin A and twin B. They are two different instances, yes, they look same, but if you are talking twin A, you are not talking about twin B and vice versa. The point is, they are not EQUAL :), it is same in Java. Twin A is equal only to itself, no1 else.

You have to create your own method, which compares all properties of two instances of Osoba.

The other option is to override the equals and hashCode methods, but it is not good approach in your case.

libik
  • 22,239
  • 9
  • 44
  • 87
0

Osaba should implement equals:

@Override
public boolean equals(Object other) {
    return imie.equals(((Osaba) other).getImie())
        && nazwisko.equals(((Osaba) other).getNazwisko()));
}

If you do not implement equals, the default implementation will be used.

Manu
  • 1,474
  • 1
  • 12
  • 18
  • 1
    You will get an exception if `other` is of another type or null. – Alex Salauyou Mar 30 '15 at 15:45
  • Obviously. Why would you ever call equals with an object of another class? – Manu Mar 30 '15 at 15:47
  • 1
    Because `equals()` is designed to accept object of any class – Alex Salauyou Mar 30 '15 at 15:50
  • Sure. But I'm never going to use it like that. So why would I add an instanceof check if it will always return true in my code? That's just a waste of time, in typing and execution. – Manu Mar 30 '15 at 15:54
  • 1
    @Manu You are free to code however you want to in your own projects, no one is going to stop you (we will continue to disagree but that doesn't matter). However for posting answers that other people are going to use on SO you need to follow the guidelines defined by the language community (in this case java) or you will receive downvotes. – chancea Mar 30 '15 at 15:59
  • You won't maybe. But imagine your code then is supported by someone else who expects that `equals()` will return false, not an exception, for another class object, because in most cases (look at Java core sources for example) it is implemented based on best practice. Such things form tiny border between average programming and good programming. – Alex Salauyou Mar 30 '15 at 16:00
0

Responds from @libik and @Code Whisperer helped me, so if anyone has the same problem, read and try to understand, it helped for me. I like that twins allegory :) It's kind of the same situation when I tried to compare two Strings.

Dargenn
  • 372
  • 4
  • 11