Computer Science Canada Searching for text from a file |
Author: | eklypze [ 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. ![]() |
Author: | Hikaru79 [ Wed Dec 14, 2005 4:34 pm ] |
Post subject: | |
The simplest, although not neccesarily best, way to go about it is the indexOf() method of String objects. Look that one up ![]() |
Author: | eklypze [ Wed Dec 14, 2005 5:01 pm ] |
Post 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. ![]() |
Author: | Hikaru79 [ Wed Dec 14, 2005 6:31 pm ] |
Post subject: | |
eklypze wrote: 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. ![]() Close ![]() 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 ![]() |
Author: | wtd [ Wed Dec 14, 2005 7:24 pm ] | ||
Post 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.
|
Author: | eklypze [ Wed Dec 14, 2005 7:54 pm ] |
Post subject: | |
Thanks a lot for the help Hikaru79. ![]() As for wtd, I have no idea what you're trying to say. ![]() |
Author: | wtd [ Wed Dec 14, 2005 7:58 pm ] |
Post subject: | |
Well, that's Ruby, but Java has support for regular expressions as well. |
Author: | Hikaru79 [ Wed Dec 14, 2005 11:25 pm ] |
Post 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? ![]() |
Author: | wtd [ Wed Dec 14, 2005 11:31 pm ] |
Post subject: | |
I have no problem. Linking back to the original post would be good. |
Author: | eklypze [ Thu Dec 15, 2005 4:42 pm ] |
Post subject: | |
Quote: Don't know what a regular expression is?
![]() |
Author: | wtd [ Thu Dec 15, 2005 5:33 pm ] |
Post 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. |
Author: | eklypze [ Thu Dec 15, 2005 7:14 pm ] |
Post subject: | |
Thank you. ![]() |