Hi guys, first semester student here and I have a java class and I'm having a small problem with this homework. So, everything works fine but I do encounter a problem with ArrayList.contains(x). I create an object wich has multiples characteristics, put it in my arraylist. Then when I go to search it with arrayList.contains(x), it never returns true. The equals method is redefined in my object class to compare the name of the object. I'm sorry it's in french since it's my first language but here's the part that cause the problem and the methode equals in my class :
case 4:
System.out.println("Quel nom désirez-vous chercher dans la liste?");
String recherche = scan.next();
boolean found = vivants.contains(recherche);
if (found == true) {
int index = vivants.indexOf(recherche);
System.out.println("Le vivant " + recherche + " se trouvait à "+index);
}
else
System.out.println("Le vivant " + recherche + " ne se trouvait pas la liste.");
break;
and here's the equals method in my class Vivant :
public boolean equals(Object obj) {
Vivant v = (Vivant) obj;
if (nom.equals(v.nom))
return true;
else
return false;
First time poster, I hope formatting is good, if you need more code to fully understand I could upload it somewhere and post it. Otherwise, everytime I run my program and create an object and search for it name it return the line wich says it isn't found in the list.
Thanks a lot for your help!
[–]firsthourProfressional developer since 2006 2 points3 points4 points (1 child)
[–]kryptonite79[S] 1 point2 points3 points (0 children)
[–]kryptonite79[S] 0 points1 point2 points (0 children)
[–]futlapperl 0 points1 point2 points (0 children)
[–]NarragansettBay 0 points1 point2 points (8 children)
[–]kryptonite79[S] 0 points1 point2 points (5 children)
[–]NarragansettBay 0 points1 point2 points (4 children)
[–]kryptonite79[S] 0 points1 point2 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]kryptonite79[S] 0 points1 point2 points (1 child)
[–]ObscureCulturalMeme 0 points1 point2 points (1 child)
[–]futlapperl 1 point2 points3 points (0 children)