Author |
Message |
eklypze
|
Posted: Wed Dec 14, 2005 4:24 pm Post subject: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Hikaru79
|
Posted: Wed Dec 14, 2005 4:34 pm Post subject: (No subject) |
|
|
The simplest, although not neccesarily best, way to go about it is the indexOf() method of String objects. Look that one up |
|
|
|
|
|
eklypze
|
Posted: Wed Dec 14, 2005 5:01 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
|
Hikaru79
|
|
|
|
|
wtd
|
Posted: Wed Dec 14, 2005 7:24 pm Post subject: (No subject) |
|
|
Hikaru79 wrote: 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.
code: | >> "mathematics" =~ /the/
=> 2
>> "mathematics" =~ /\b the \b/x
=> nil
>> "Swab the deck, matey!" =~ /\b the \b/x
=> 5 |
|
|
|
|
|
|
eklypze
|
Posted: Wed Dec 14, 2005 7:54 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
|
wtd
|
Posted: Wed Dec 14, 2005 7:58 pm Post subject: (No subject) |
|
|
Well, that's Ruby, but Java has support for regular expressions as well. |
|
|
|
|
|
Hikaru79
|
Posted: Wed Dec 14, 2005 11:25 pm Post subject: (No subject) |
|
|
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? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Dec 14, 2005 11:31 pm Post subject: (No subject) |
|
|
I have no problem. Linking back to the original post would be good. |
|
|
|
|
|
eklypze
|
Posted: Thu Dec 15, 2005 4:42 pm Post subject: (No subject) |
|
|
Quote: Don't know what a regular expression is?
|
|
|
|
|
|
wtd
|
Posted: Thu Dec 15, 2005 5:33 pm Post subject: (No subject) |
|
|
eklypze wrote: Quote: Don't know what a regular expression is?
http://www.compsci.ca/v2/viewtopic.php?t=6666
Search General Programming forum for "regex-tut" with "wtd" as the author. |
|
|
|
|
|
eklypze
|
Posted: Thu Dec 15, 2005 7:14 pm Post subject: (No subject) |
|
|
Thank you. |
|
|
|
|
|
|