This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

searching thru arrays

 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array stuffed with filenames from a hard drive. What I need to do is search thru the array for only an "occurence" of the text being searched on. (Kind of like a web search engine). What would be the easiest way to search this array for a particualr instance of a word.
(i.e. if I want to find all the filenames in the current directory that have the word "text" in the title, I would type the word text in the search bar and the results would spit out all the filenames containing the word text?)
So far I know that I would have to take all entries in the array and make them lowercase and do the same to the search text that way the search it case insensitive, but how do I search this list?
Thanx
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
This is neither pretty nor fast, but it does the job you ask for and you don't have to worry about case (because we use equalsIgnoreCase()):

Hope this helps.
Stephanie
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that looks like it will do just fine. I will let you know how it comes out...
Thanks...
 
Matts Smith
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not use indexOf ??? Have I missed the point?
why no break after finding a match?
why do you create 2 string objects in every loop iteration?
OUCH
[This message has been edited by Matts Smith (edited February 28, 2001).]
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to break after finding a match because I want to find all occurrences in the list, not just the first one. So that part is good.
The one thing I don't understand it which part of the code is the piece that cycles thru the entire file name. the way it sort of looks to me is that it only compares itself to the first part of the filename. however we know it compares the whole thing because I've used this and it works like a charm!!
indexOf might have worked...I didn't even know that method existed.
Either way, its working and it isn't too slow, but I will play with indexOf to see if I can streamline it.
thanx....
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt,
You have some good points:

why not use indexOf ??? Have I missed the point?

You are right, indexOf() is simpler and more elegant. However, Ryan seemed to want something that is not case sensitive. indexOf() will not work for that.

why no break after finding a match?

He wanted to find all files in the list whose name contained the substring, not just the first.

why do you create 2 string objects in every loop iteration?

You are right ... I'm sure there is a much cleaner way to do this. As I said in my first post "This is neither pretty nor fast". I was just trying to get Ryan a quick answer so he could continue. If you want to provide better version for him, that would be great.
Stephanie
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I probably could have used indexOf() if I had first forced all strings to lower case before comparing them. I may go back and look into that.
For now though, Steph did a great job and it works. Thanks Steph...i can call ya Steph right?
 
Matts Smith
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why no break after finding a match?
I mean't after retVal = true;
lata
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we put a break after the retVal = true, wouldn't that kill the loop that is checking for matches??
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Matts is suggesting is this:

although I'm not sure because it still has the problem of creating new String each time through the loop (because of toLowerCase()).
But it will work.
Stephanie
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what I have works well...what I needed was this:
I have a ton of MP3 files on my hard drive. When I am in the mood to hear a particular song, I have to scroll thru the whole directory to find the file that I want. I know I coudl probably use the normal windows find, but I just wanted to see if I could do it. It still needs work if it were to be for anyone to use, but right now its compiled strictly for my PC. I could add a few features to select the default MP3 player, etc, but for now its hard coded for my machine.
It works great though, I type in any part of the title, select the directory, pick the song from the resulting list and it kicks of windows media player to play the song.
Thanks for your help!!
Ryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic