Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Searching for text from a file
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
eklypze




PostPosted: Wed Dec 14, 2005 4:24 pm   Post subject: Searching for text from a file

Hello everyone! Smile
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. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: 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 Smile
eklypze




PostPosted: 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. Very Happy
Hikaru79




PostPosted: Wed Dec 14, 2005 6:31 pm   Post subject: (No 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. Very Happy


Close Smile 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 Smile

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 Smile
wtd




PostPosted: 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 Smile


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




PostPosted: Wed Dec 14, 2005 7:54 pm   Post subject: (No subject)

Thanks a lot for the help Hikaru79. Smile 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




PostPosted: 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




PostPosted: 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? Smile
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: Thu Dec 15, 2005 4:42 pm   Post subject: (No subject)

Quote:
Don't know what a regular expression is?


Embarassed
wtd




PostPosted: Thu Dec 15, 2005 5:33 pm   Post subject: (No subject)

eklypze wrote:
Quote:
Don't know what a regular expression is?


Embarassed


http://www.compsci.ca/v2/viewtopic.php?t=6666

Search General Programming forum for "regex-tut" with "wtd" as the author.
eklypze




PostPosted: Thu Dec 15, 2005 7:14 pm   Post subject: (No subject)

Thank you. Very Happy
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: