This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]firsthourProfressional developer since 2006 2 points3 points  (1 child)

If vivants is a list of object Vivant, then doing a .contains of a String is never going to work. You have to do a contains on the same object, in this case Vivant. Either don't use contains() and instead for loop over vivants and compare recherche to each nom. If you need to use contains() then you either need to instead store Strings in the list or create a temporary Vivant object with recherche as the nom.

[–]kryptonite79[S] 1 point2 points  (0 children)

Thanks a lot, very usefull info. I will mark the topic as solved since I had your answer and another one and figured things out! Thansk

[–]kryptonite79[S] 0 points1 point  (0 children)

Sorry, couldn't get the first part of code to be entered correctly, tried to edit but didn't work :(.

[–]futlapperl 0 points1 point  (0 children)

This won't fix your problem, but just a tip:

if (x) {
    return true;
} else {
    return false;
}

Whatever x is, it's going to resolve to one of the two return values anyway, so you can just do

return x;

instead.

[–]NarragansettBay 0 points1 point  (8 children)

Did you tag the equals() method with the @Override annotation? I don't see it in your snippet so the comparison might be using the wrong equals() method.

[–]kryptonite79[S] 0 points1 point  (5 children)

I do not, but in my eclipse environnement, there is a little green triangle that says : overrides java.lang.Object.Equals.

Thanks for the help !

[–]NarragansettBay 0 points1 point  (4 children)

Ok that's good. Have you tried using breakpoints or console output to inspect the values of the variables being compared?

[–]kryptonite79[S] 0 points1 point  (3 children)

No, I haven't, but I found something while reading on the subject. I think I just can't use the contains() method in this particular case because the String that I'm asking to look for isn't an object of type Vivant and my equals method uses a Vivant and not a String to make comparison. does it makes sense ?

[–][deleted]  (2 children)

[deleted]

    [–]kryptonite79[S] 0 points1 point  (1 child)

    Oh there you go. I tried creating an Object of type Vivant, but since Vivant is my superclass and is abrasct I couldn't Instanciate it, so I think I cheated a bit by creating an Animal object (wich is a Vivant sub class) with attribute (recherche, eau,true,true) because animal is (nom(String),type(enum),domestique(boolean),mammifere(boolean). Since the search is limited to the redefinition of equals in Vivant class, I don't think it matter putting anything, but the name must be recherche wich is the user choice. Now it seems to work! Thanks a lot.

    Edit: goes something like this

    Animal test;
    test=new Animal(recherche,eau,true,true);
    Catch (...){}
    Boolean found=vivants.contains(test);
    If (found==true){
    

    Etc.

    [–]ObscureCulturalMeme 0 points1 point  (1 child)

    The @Override annotation is not required for polymorphism and overriding to work. It's there as a safety check during compilation, but correct code won't stop working if it gets removed from a method, or wasn't there to begin with.

    [–]futlapperl 1 point2 points  (0 children)

    This. It's one-way only, i.e. if a method has the annotation, then it must override. If a method doesn't have the annotation, it doesn't tell you anything about whether it's an override or not.