Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Check if arraylist contains text

  1. #1
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Check if arraylist contains text

    Hi guys, it's me again! =)

    This time I'm wondering how I can check if an arraylist contains "something".
    I do this by using ".contains", and it looks like that:
    Java Code:
    			boolean sok = false;
    			if(ansatte.contains(tekstFelt.getText())){
    				sok = true;
    			}
    			
    			if(sok == true){
    				System.out.println("IN IF !!!!");
    				tekstFelt2.setText(nyAnsattAnsatt.getLastname());
    				tekstFelt3.setText(nyAnsattAnsatt.getEmail());
    				tekstFelt4.setText(String.valueOf(nyAnsattAnsatt.getTlf()));
    				tekstFelt5.setText(String.valueOf(nyAnsattAnsatt.getR_number()));
    				
    			} else{
    				System.out.println(tekstFelt.getText() + "not found in ansatteliste." + sok);
    			}
    		}

    I don't understand why does it always return false? :S

    Check if arraylist contains text-img.png
    First is name inputed in tekst field, and second is arraylist ... Looks like arraylist contains name "Damian", but it returns false anyway.
    Thanks for Your response!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default Re: Check if arraylist contains text

    Check if your List really contains that String, not something that just looks like it; e.g. print your entire List as follows and see for yourself:

    Java Code:
    for (String element : yourList)
       System.out.println("["+element+"]");
    kind regards,

    Jos
    OPTYMISTA likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Check if arraylist contains text

    The List interface specifies that contains() checks for object equality. So they must be the same object to return true.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default Re: Check if arraylist contains text

    Quote Originally Posted by jim829 View Post
    The List interface specifies that contains() checks for object equality. So they must be the same object to return true.
    Not so; the API docs say: "More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))." The String class implements the equals( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Check if arraylist contains text

    But the String class does not implement the List interface. The signature of the List interface shows equals(Object o). And I checked the source and it leads to evaluating this == obj as the return value. I was talking about how the List interface was implemented.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default Re: Check if arraylist contains text

    Quote Originally Posted by jim829 View Post
    But the String class does not implement the List interface. The signature of the List interface shows equals(Object o). And I checked the source and it leads to evaluating this == obj as the return value. I was talking about how the List interface was implemented.

    Regards,
    Jim
    You've got it all backwards: the List implementation used the equals( ... ) method in its elements to test whether or not an Object is in the List. The String class implements the equals( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Check if arraylist contains text

    Thanks for your responses, I really grateful to all of You! I've figured it out! My arraylist is of own class which I built myself. So i just did it like this:

    Java Code:
    for (Employee element : myArrayList)
       if(element.getName().equals(tekstFelt.getText())){
     >> DO SOMETHING <<		
    				}
    }

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Check if arraylist contains text

    You are absolutely correct! I think I shall take a break from this for a while as I pick up the pieces.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2012, 10:34 PM
  2. Enable/Disable Text Field upon checking check box
    By DGates in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 01:07 AM
  3. age old problem - best way to check if file is text
    By madroadbiker in forum Advanced Java
    Replies: 11
    Last Post: 05-27-2011, 06:57 PM
  4. Replies: 2
    Last Post: 04-28-2008, 10:26 AM
  5. how do i write to a text file from an arraylist?
    By otoro_java in forum New To Java
    Replies: 3
    Last Post: 01-30-2008, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •