• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Filename search with multiple keywords

 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

For example:

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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming searchname.searchname is a String, what methods do you think you could use in java.lang.String (<= API Link) that would help you split the text into multiple parts? What do you think you should use as a delimiter to split on? When you finally do split it, you will have a String[] with search terms, so you would need to loop through each possible search term to see if it is found, and return true if any one of them are.
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you click on the API Link for java.lang.String I gave you?
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

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?

Thanks,
Joel
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also tried:



But still it only shows results for the first word only
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you are immediately returning the results of the first search. You have to consider what happens when the first search fails. What to do then? If I were you, I would step away from code, bring out a pencil and paper and start writing the logic out in close-to-plain-english first. Once I was sure of the logic and sequence then I would turn it into code. Maybe I would start with:

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.
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been looking into the loop more and think I know why it is only showing the first term. It is with the following code:



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.
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my latest effort:



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)

Thanks
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have since worked out that I was trying to store a string in a boolean which was giving me the error.

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?
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally have working code:



Big thanks Steve and also JackyZhu from another forum for pointing me in the right directions. I have learnt a lot more about loops, booleans and arrays. Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic