
-----------------------------------
eklypze
Wed Dec 14, 2005 4:24 pm

Searching for text from a file
-----------------------------------
Hello everyone!  :) 
I am doing my final project for the year in Java at the moment. I have barely gotten started right now, because I am still trying to work on some basic concepts before I decide to continue.

The project I have chosen to do is a PC Dictionary where a user enters a word into a search box and the program will search through a notepad file for  that word and returns the definition for it.

Although my problem at this point is that I am able to retrieve text from a file, but I am uncertain on how to search for that particular word from the text file. Does anyone have any suggestions on how I am able to approach this?

Thanks.  :D

-----------------------------------
Hikaru79
Wed Dec 14, 2005 4:34 pm


-----------------------------------
The simplest, although not neccesarily best, way to go about it is the indexOf() method of String objects. Look that one up :)

-----------------------------------
eklypze
Wed Dec 14, 2005 5:01 pm


-----------------------------------
Thanks for your suggestion. Although I am still a little unsure of how I can apply the indexOf() method to search for the word a in the text file. Doesn't the indexOf() method only work if you want to find the position of one character in a String? Or is there another use of this method that I should know about?

Thanks again.  :D

-----------------------------------
Hikaru79
Wed Dec 14, 2005 6:31 pm


-----------------------------------
Thanks for your suggestion. Although I am still a little unsure of how I can apply the indexOf() method to search for the word a in the text file. Doesn't the indexOf() method only work if you want to find the position of one character in a String? Or is there another use of this method that I should know about?

Thanks again.  :D

Close :) It's not neccesarily one letter though -- it finds the position of one STRING in another String. It returns -1 if the String isn't present in the other String.

Therefore, if you check the indexOf() of a smaller String in each line of your dictionary, and it *doesn't* return -1, the String must be present in that line :)

Of course, there's problems with this approach. For example, if you're searching for the word 'the', and the line contains 'mathematics', it will give you a position. This was just an idea to get you started :)

-----------------------------------
wtd
Wed Dec 14, 2005 7:24 pm


-----------------------------------
Of course, there's problems with this approach. For example, if you're searching for the word 'the', and the line contains 'mathematics', it will give you a position. This was just an idea to get you started :)

This calls for a regular expression.

>> "mathematics" =~ /the/
=> 2
>> "mathematics" =~ /\b the \b/x
=> nil
>> "Swab the deck, matey!" =~ /\b the \b/x
=> 5

-----------------------------------
eklypze
Wed Dec 14, 2005 7:54 pm


-----------------------------------
Thanks a lot for the help Hikaru79.  :) With your advice, I think now I might to able to try and solve the rest of this myself by playing around and experimenting with the indexOf() method.

As for wtd, I have no idea what you're trying to say.  :wink:

-----------------------------------
wtd
Wed Dec 14, 2005 7:58 pm


-----------------------------------
Well, that's Ruby, but Java has support for regular expressions as well.

-----------------------------------
Hikaru79
Wed Dec 14, 2005 11:25 pm


-----------------------------------
See, this is the sort of thing the Wiki would be great for. Don't know what a regular expression is? *Zap* here's a link to a great compsci.ca-original article on them, with specific examples from the major languages used here.

I think I'll get around to porting some of the better tutorials to the Wiki tomorrow. wtd, I'm assuming you won't mind if I migrate your awesome tutorials wholesale? :)

-----------------------------------
wtd
Wed Dec 14, 2005 11:31 pm


-----------------------------------
I have no problem.  Linking back to the original post would be good.

-----------------------------------
eklypze
Thu Dec 15, 2005 4:42 pm


-----------------------------------
Don't know what a regular expression is?

 :oops:

-----------------------------------
wtd
Thu Dec 15, 2005 5:33 pm


-----------------------------------
Don't know what a regular expression is?

 :oops:

http://www.compsci.ca/v2/viewtopic.php?t=6666

Search General Programming forum for "regex-tut" with "wtd" as the author.

-----------------------------------
eklypze
Thu Dec 15, 2005 7:14 pm


-----------------------------------
Thank you.  :D
