This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String Search Using Java.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Search Using Java...." Watch "String Search Using Java...." New topic
Author

String Search Using Java....

Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Hi Everyone,

I am trying to search a text file using JAVA. But my problem is i need exact matches of the occurrences. That is not happening. I am trying with contains() method to search for the string. For example...

I have a file which contains

John is good boy.
Marie is a good girl.
Jack is good boy.
Johnny is good boy.
Johnson is good boy.

I am using the search string as John.

I need the output as :
John is good boy.

The problem is I am getting the output as:

John is good boy.
Johnny is good boy.
Johnson is good boy.

Coz they all contain John. I want the out just as:
John is a good boy.

Is there any way to match the exact string.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

A quick solution will be,

1. Get a line as a string.
2. Use String#split(" ") OR StringTokenizer class, and get a string array on "sapce", so that you get words separated.
3. Match for the occurrence on every array element.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Could you please provide some of the syntax for the process.

[ UD: Please start a new thread for questions that are unrelated to the original topic. ]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Somnath Mallick wrote:Could you please provide some of the syntax for the process.

Whatever you're looking for, it is at JavaDocs
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Instead of searching for "John" couldn't you just search for "John " <-- The space will only be present for the selection you are looking for. The other method would be as Sagar mentioned to use a String Tokenizer. Creating tokens would most likely be your best bet

SCJA
~Currently preparing for SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38052
    
  22
Or you could set up a regular expression; you can set up tags for "word boundary" so you put John between two of those tags. I have forgotten what you use for the tags, but it will all be in the link I gave you.
 
jQuery in Action, 2nd edition
 
subject: String Search Using Java....
 
Similar Threads
JXL search for cell according string value
How to search a file???
Hi
How to search a file which contains the string either *= or =*
Problem with recognizing the class files