3

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? :)

11
  • with just the given code it´s pretty impossibl to tell (though i´m guessing Object#equals didn´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. Commented Jan 11, 2017 at 12:16
  • Please, give an input example.. Commented Jan 11, 2017 at 12:18
  • Can you please provide an example of the name searched? and the readString method as well Commented Jan 11, 2017 at 12:18
  • 2
    Clearly need a minimal reproducible example or at least the name and Team values Commented Jan 11, 2017 at 12:19
  • 2
    I would encourage you to learn to debug this yourself. If A does not equal B but you're convinced they are equal then why don't you print out the values of A and B just before the comparison? Maybe they're not what you expected them to be? Commented Jan 11, 2017 at 12:54

1 Answer 1

1

You are reading from your Scanner with this line :

return keyboard.nextLine().toLowerCase();

And are getting the full name from

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);
}

On one side you for the input to be lowercase, on the other side you force the full name to Have uppercase in first character. This will never be equals.

You can

  • call String.toLowerCase() on getFullName() result since the input is already in lowercase both will match,
  • use String.equalsIgnoreCase(String) to ignore the case on both values

Note : The last one is safer because you don't need to bother to set yourself a specific case (lower or upper), it will be done in the method.

Sign up to request clarification or add additional context in comments.

Comments

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.