lundi 30 mars 2015

Comparing objects never returns true


Vote count:

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.



asked 17 secs ago







Comparing objects never returns true

Aucun commentaire:

Enregistrer un commentaire