0

guys so I have this method that I am trying to construct, I am just having a hard time understanding the logic. This is the condition of the method:

public int search(String str) – search the list for parameter str. Searches should work regardless of case. For example, “TOMATO” is equivalent to “tomato.”

Hint: the String class has a method called equalsIgnoreCase. If the string str appears more than once in the ArrayList, return the first index where the string str was found or return -1 if the string str was not found in the ArrayList.

This is what I have so far for my code, I am not sure if this is the right way to do it. My ArrayList is defined as words.

In order to solve this issue, I am thinking of using a foreach statement to iterate through the ArrayList then an If to check if the words match then return the Index value based on the match but I am getting error. The other confusion I am having is how do I only return the first Index value only. Maybe I am doing this wrong. Any help or direction is appreciated.

 public int search(String str)
    {
        for(String s : words)
        if(s.contains(s.equalsIgnoreCase(str)))
            return s.get(s.equalsIgnoreCase(str));
            
    }
Murtaza Mohsin
  • 142
  • 1
  • 12
  • In order to get the index you need to use a normal loop instead of a for-each loop; In order to fix your error you need to state the exact error text and position, see at https://stackoverflow.com/questions/8439037/better-way-to-find-index-of-item-in-arraylist for a working example – MDK Nov 14 '20 at 20:21

3 Answers3

1

You actually overcomplicated it a little bit

public int search(String str) {
    for(String s : words) {
        if(s.equalsIgnoreCase(str)) {
            return words.indexOf(s);
        }
    }
    return -1;
}

Since the return method will stop running more code in the function it will always return the first matching word.

Matt
  • 111
  • 7
  • What did you do? Did you delete and re-enter your answer to strip the comments off of it? That's shifty. It's the same answer though. This answer still has the problem of doing an extra search through the list. It is still an answer that I would recommend people not use as a proper way to solve this problem, when it's so easy to avoid the need for that second walk through the list. – CryptoFool Nov 14 '20 at 21:52
1

The first answer unnecessarily has to search through the list of words to find the index once it has determined that the word is in the list. The code should be able to already know the index. This is the more efficient approach:

public int search(String str) {
    int i = 0;
    for (String s : words) {
        if (s.equalsIgnoreCase(str))
            return i;
        i++;
    }
    return -1;
}

There is also the more classic approach...the way it might have been done before the enhance for loop was added to the Java language:

public int search(String str) {
    for (int i = 0; i < words.size(); i++)
        if (words.get(i).equalsIgnoreCase(str))
            return i;
    return -1;
}
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Hmm, so it seems like java by default returns the first value so there isn't any extra condition I need to write. This is actually solved my problem. Thank you so much! Can I ask why the foreach loop is not the right thing to use for a problem like this? – Murtaza Mohsin Nov 14 '20 at 20:35
  • The "for each loop" doesn't give you the index of each item in the list, and that's ultimately what you want once you find a matching entry. Doing it this way means you have the index once you find the item, because you used that index to refer to the item. – CryptoFool Nov 14 '20 at 20:37
  • @Matt - Of course. These sorts of questions are about learning how to program. They aren't real life. But in learning how to program, one should learn the best way to accomplish a problem so that when they use the same techniques in real life, they pick the best algorithm for the job. Your version, in real life, could slow certain programs down considerably vs doing the same sort of search the more correct way. – CryptoFool Nov 14 '20 at 20:38
  • 1
    @MurtazaMohsin - See my revised answer. It IS quite possible to use a "for each" loop and have it be a correct thing to use. In fact, I decided that I prefer the solution that uses it. – CryptoFool Nov 14 '20 at 20:59
0

You can use stream also to resolve this problem:

 public boolean search(List<String> words, String wordToMatch)
 {
    Predicate<String> equalityPred =  s -> s.equalsIgnoreCase(wordToMatch);
    return words.stream().anyMatch(equalityPred);
        
 }
Parag Goel
  • 78
  • 8