0

I have a class called Paragens like this:

public class Paragens {
    static int contadorParagens = 1;

    String nomeParagem;
    int id;

    public Paragens(String nomeParagem) {
        this.nomeParagem = nomeParagem;
        this.id = contadorParagens++;
    }

    // getters and setters for nomeParagem
}

Every Paragens object has a name and an Id.

This class has a main method where I create several Paragens objects and store them inside an ArrayList like this:

public static void main(String[] args) {
    ArrayList<Paragens> paragens = new ArrayList<Paragens>();
    paragens.add(new Paragens("name1");
    // ... add more paragens
}

This is working ok. If I insert a bunch of paragens and print them I can see tat is all ok.

What I am trying to do is to ask the user to input a paragem name and then I want to see if that paragem is already in the ArrayList.

String name;
System.out.println("Insert paragem name: ");
pickName = sc.nextLine();
System.out.println(paragens.contains(pickName));

What am I doing wrong?

3 Answers 3

10

contains checks to see if the list contains the actual thing you handed it. In this case, you're passing in a String name, but comparing it to Paragem instances. contains can't magically guess that it's supposed to look at a given property on the Paragem instances to compare the string.

You can easily loop the list to find out for yourself:

boolean found = false;
for (Paragem p : paragems) {
    if (p.nomeParagem.equals(pickName)) { // Or use an accessor function for `nomeParagem` if appropriate
        found = true;
        break;
    }
}

...or as a function:

boolean containsParagemWithName(ArrayList<Paragem> paragems, String pickName) {
    for (Paragem p : paragems) {
        if (p.nomeParagem.equals(pickName)) {
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

5 Comments

And if you make that into a method of its own, you can do away with the found flag and just return true in the loop or return false after it.
@Anonymouse: Don't override equals to make a Paragem equal a String. That would be very unconventional (for example equality would then no longer be symmetric). Equals, hashCode and these methods are very general-purpose, expect to follow certain contracts, and should not do unexpected things to accommodate some special purposes.
Yeah, for equals he should create a new Paragem, and then check if that one is equal, obviously.
Ah, okay, that would be fine (if name equality does indeed mean Paragem equality).
Thanks for the explanation. That makes all the sense. Unfortunately I did'n remember doing the for loop. Will implement that.
1

Well, you need to implement the contains method yourself. Do a for loop over the entire array and check if the name of one of the elements is equal with what you're trying to add. If not, add a new Paragens(pickName).

1 Comment

There is nothing wrong with the contains method. He just needs to either implement equals or manually inspect the instances in a loop.
1

Objects by default are compared by their memory location. So if you have two Paragem with the same name, they are still not equal.

So either, you check the name of each one:

boolean checkDuplicate(String pickName) {
    for (Paragem p : paragems) {
      if (p.nomeParagem.equals(pickName)) return true;
    }
    return false;
}

or implement (override) the equals method to compare names (you should be calling contains on a new Paragem object then instead of a String, though).

3 Comments

"Objects by default are compared by their memory location. So if you have two Paragem with the same name, they are still not equal." Yes, but it's not comparing two Paragem instances. He's comparing String to Paragem.
@T.J. Crowder: Correct. Which is why it even works less. ;-) Unfortunately, contains in Java Collections is defined for Objects, which introduces this kind of bugs.
Understood the for loop but the last thing (implement (override) the equals method to compare names.) didn't understand because I'm new at Java. Nevertheless will implement the for loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.