File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes match the word  from text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

match the word from text file

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi all,

i have an input like this in the text file

located 1
biosphere 2
reserve 3

in this i want to find if the text file has the word located. It should match the word and display the result as 1. How to do like that ? Help me...

Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

You can do the following:
1) you input the text file, use readLine() to read the lines in the text file one by one.
2) after that you can use stringTokenizer class. You get two tokens at each line.
3) Store the tokens in a data structure. for eg tree map: TreeMap<String, Integer> = new TreeMap<String, Integer>();
4) once you have all the values in the tree map, you can just get the value for a particular string. The map would have entires like: Located, 1 ; biosphere 2 and so on.
So for Located you will get the value 1.

I did this for my project. I had a text file with some numerical values which were required for some calculation. So i read the file, used StringTokenizer to get the tokens and stored them in the tree map.

Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8951

deepika deepi wrote: i have an input like this in the text file

Deepika, this is a duplicate of this thread. Please don't double-post.

Locking this one.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link:
subject: match the word from text file
It's not a secret anymore!