I'm trying to find an object in an array list that's in another class by using the name of the object.
private void listParticipant() {
System.out.print("Put in name of participant to see if currently registered: ");
String name = readString();
for (Team team : teamList) {
if (team.hasParticipantWithName(name)) {
System.out.println("Participant is registered.");
return;
}
}
System.out.println("Participant does not exist.");
}
But even though I know the list contains the name I'm putting in, it returns "participant does not exist".
This is the method in the team-class I'm using to find the name:
public boolean hasParticipantWithName(String name) {
for (Participant part : participantList) {
if (name.equals(part.getFullName())) {
return true;
}
}
return false;
}
And this is my getFullName method in another class, that contains the constructor for participants.
public String getFullName(){
String capitalizedLastName = lastName.substring(0, 1).toUpperCase()+lastName.substring(1);
String capitalizedFirstName = firstName.substring(0, 1).toUpperCase()+firstName.substring(1);
return (capitalizedFirstName + " " + capitalizedLastName);
}
The objects are added using constructor
public Participant(String firstName, String lastName, int id)
and input is then for example "john smith" and "John Smith"
My read string is
private String readString() {
return keyboard.nextLine().toLowerCase();
}
I'm not sure how to fix it.. I've tried calling name by lowercase and uppercase first characters, but the method returns "does not exist" on everything. Any ideas? :)
Object#equalsdidn´t get overriden). You might want to include a MCVE in order for us to be able to reproduce that behaviour and be able to respond with an answer.readStringmethod as well