aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

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.


OCPJP
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7653
    
  19

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.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: match the word from text file