File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes searching thru arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "searching thru arrays" Watch "searching thru arrays" New topic
Author

searching thru arrays

ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
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


Ryan Headley<br /><a href="http://www.sudovi.com" target="_blank" rel="nofollow">http://www.sudovi.com</a>
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
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

Joined: Jun 28, 2000
Posts: 156
Wow, that looks like it will do just fine. I will let you know how it comes out...
Thanks...
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
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

Joined: Jun 28, 2000
Posts: 156
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

Joined: Jun 14, 2000
Posts: 347
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

Joined: Jun 28, 2000
Posts: 156
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

Joined: Feb 03, 2001
Posts: 113
why no break after finding a match?
I mean't after retVal = true;
lata
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
If we put a break after the retVal = true, wouldn't that kill the loop that is checking for matches??
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
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

Joined: Jun 28, 2000
Posts: 156
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: searching thru arrays
 
Similar Threads
problem with search method
word compare
searching a large list of words
Search large xml files
Finding the filenames of all the files in a directory