I have a recursive search that uses the ".contains" function to find file names based on the search words entered. The only problem is that if the file name contains only one of the words it doesn't show up.
Keywords entered = "black cat"
If the title contains "black cat" in that specific order it returns the file name. However if the title only contains "cat" it doesn't return anything.
I need some way to break the search keywords up so that it finds all the files that contain any of the keywords.
Here is the section of code that handles the search:
Below is the full code for that activity:
Thank you in advance for any help you can offer
Thanks for you help. I do understand that I need to split the search term up and check if they filenames contain any of the search string words. Unfortunately I am at a loss as to how I go about this. You are correct that searchname.searchname is a string that has been passed to the activity from a previous search screen. This is the search term that the user has entered.
Unfortunately my Java skills are not at the level that seems to be required to carry out this task. I understand the logic of what I need it to do, I just don't know how to go about this.
I have had another look at the link you posted. I think I am very close. I believe I can use the ".split" function.
My new code is
The only problem is that this only searches on the first word in the search string. For instance if I put "black cat" as a search string it will bring up any file with "Black" in the title. I think it isn't checking every word in the string split. Can you please help me out with how I can achieve this?
1) Split the search term into words.
2) For each word
2a) Check if the word is found in the file name
2b) If the word is found, return true
2c) If the word is not found, keep searching
3) If done searching all words, and have not returned from this method, return false.
The "return" section is ending the loop - this is why it is only showing the first one and also why I get a "dead code" error for the "i++" as "i" is never incremented.
This being said, I still cannot work out how to return each "name" that contains a word form the spilt in its filename to my array.
I think I need to store each "name.toUpperCase().contains(tempname[i])" to a list and then once the for loop is finished checking all the words return all of the values. I just don't know how to do this.
I am trying to store the name values while in the loop and then return all the values after the loop has finished. I get an error of "Type mismatch: cannot convert from boolean to String" for the code and an error of "Type mismatch: cannot convert from String to boolean" for the code.
If you can offer anymore assistance that would be greatly appreciated. Like I said before, my java is quite basic. I have written this app purely in my own time for my workmates to use for free - java is not my profession (As you can tell lol)
If I change the code to :
I get an error in the "return namestring[i]" code for the 'i' value. If I move the line up under the "namestring[i] = name.toUpperCase().contains(tempname[i]);" (in the same brackets) I lose the error but the loop won't loop because it is closing the loop after the first pass. Any ideas how I can get around this?