Kulahan
Kulahan

Reputation: 522

Java .contains not actually searching my arraylist

Edit: re-writing the whole question.

public static int search(ArrayList addressBook)//handles all search functionality
    {
        Scanner input = new Scanner(System.in);
        String searchModifier;//declaring variables
        int noEntry = -1;

        System.out.print("\n\nPlease enter value to search for: ");//empowering the user
        searchModifier = input.next();//gathering searchModifier
                
        System.out.print (addressBook);
                
        if(addressBook.contains(searchModifier))//if their searchModifier is found
            return addressBook.indexOf(searchModifier);//return that index value
        else
            return noEntry;
    }

Above is the function I'm having issues with. I call this from another file. When I insert System.out.print(addressBook) into the system before the "if" statement, I get the following:

arrayList output

Clearly, it's in there. Why isn't it showing up?

Upvotes: 0

Views: 104

Answers (4)

hagrawal7777
hagrawal7777

Reputation: 14678

What do you mean by "searchModifier"? It has to be same String which should be present in your ArrayList and contains is case sensitive search.
If you are trying to search just a part of string then ArrayList.contains() is not meant for it.

EDIT You can do something like below but you have to loop, that's the only catch.

for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i) != null && arrayList.get(i).toLowerCase().contains("your_match_String".toLowerCase())){
                //Match found...
            }
        }

Upvotes: 2

Erick G. Hagstrom
Erick G. Hagstrom

Reputation: 4945

ArrayList.contains(Object) does indeed search the entire ArrayList to see if it contains a value that equals() the provided argument. In your case, it is looking to see if there is a value in addressBook such that value.equals(searchModifier).

Your names are confusing, though. addressBook is an ArrayList<T> where T is the same type as searchModifier (or a supertype thereof)? Who would expect that an address book would contain search modifiers?

Now that you've modified your question, it's clear that you don't understand. You have some class, we'll call it X, with String fields (First, Last, etc.). Your addressBook variable is of type ArrayList<X> (or something similar, like maybe ArrayList<Object>). Your problem is that searchModifier isn't an X, it's a String. You're trying to find an X in addressBook that has searchModifier as a field value. That's not the same as finding searchModifier in addressBook.

You can't use the contains() method to get the result you want.

Upvotes: 1

Alvin Thompson
Alvin Thompson

Reputation: 5448

You can't use contains there because it searches for an exact, case-sensitive match. Instead, manually iterate through the entries and return the index if an element matches your criteria (substring, case-insensitive).

Upvotes: 3

AdamSkywalker
AdamSkywalker

Reputation: 11619

Your list contains Address objects, not strings.

Obviously, you can't find a string in a list of adresses.

Upvotes: 1

Related Questions