Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Searching ArrayList<Objects> for a name?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Searching ArrayList<Objects> for a name?

    I am having problem passing a test code for name search, any mistakes!?

    public boolean hasMember(String name)
    {
    for(Membership member: members)
    {
    if (member.getName().equals(name));
    return true;
    }
    return false;
    }

    this is the test case:
    public void testhasMember()
    {
    assertFalse(club1.hasMember("Jack"));

    assertTrue(club2.hasMember("Jack"));
    assertTrue(club2.hasMember("Ianto"));
    assertTrue(club2.hasMember("Owen"));
    assertTrue(club2.hasMember("Rhys"));
    assertFalse(club2.hasMember("David"));
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Searching ArrayList<Objects> for a name?

    Looks good to me.

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Searching ArrayList<Objects> for a name?

    Hi JavaCloud.

    if (member.getName().equals(name)); <---- Stray semi colon!

    Best Regards,

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  4. The Following User Says Thank You to Freaky Chris For This Useful Post:

    PhHein (October 23rd, 2013)

  5. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Searching ArrayList<Objects> for a name?

    Good catch!

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,612
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Searching ArrayList<Objects> for a name?

    Not very hard to do if you are using the -Xlint compiler option you get the following warning:
    TestCode15.java:167: warning: [empty] empty statement after if
    if (member.getName().equals(name));
                                      ^
    with this commandline:
    D:\Java\jdk1.7.0.7\bin\javac.exe -cp . -Xlint TestCode15.java
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Searching ArrayList<Objects> for a name?

    Thanks so much :-)

Similar Threads

  1. Bubblesort of an ArrayList with objects
    By bondage in forum Algorithms & Recursion
    Replies: 9
    Last Post: January 4th, 2012, 10:51 AM
  2. Really need some help with Objects inside an ArrayList
    By ISuckSoBad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2011, 12:33 PM
  3. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 01:20 PM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 07:32 PM
  5. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 07:01 PM