how do i obtain a substring from a string thats been read in from a file?

ryuturk used Ask the Experts™

i was wondering if you could assist me.  I'm new to programming and i'm currently trying to get to grips with it.

Presently i'm trying to write a program that will take in a file and compare each line in the file to a pre-defined word, just to see how many there are.

From my work i've realised that the java method - equals isn't suitable as the method means it must be an exact match.  Now, i've heard that i should use a string tokenizer but i haven't yet been able to figure out how to use it or why i need to.  Can you help me?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Go here:

select 'java.lang' in the upper left frame,
select 'String' in the lower left frame (under 'Classes')
then, in the main frame, under 'Method Summary', take a look at the 'compareTo' and 'indexOf' method overloads.
This is how you would need to do it..

open an instance of BufferedReader on a FileReader and read the contents of the file.. you will find plenty of example online.. you can check out the below link.. gives u an idea of how to read files

Once you read the line, then tokenize it using StringTokenizer (which splits the line into words).. then loop through each word and compare it using

this method compares ignoring the case..

for more info on StringTokenizr, check out

cuyen, just a suggestion, for navigating, you could have gone to 'String' and done a right click on the link.. in IE, it will give u the option of "Open Target in New Window".. this would open it in a new window with the complete URL thus saving u a lot of time of typing as to how to get to String class


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Recommendation: Split between cuynen and vemul

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial