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
Bookmark "match the word  from text file" Watch "match the word  from text file" 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...
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 297

Hi Deepika,

Is it the fixed format for file??if yes, then you can read the line with readline method and check whether it contains the word "located" or not and if it contains the word located then split string returned by the readline function based on space and get the numeric value.
use bufferreader and inputstream reader for file reading.

For this file format should be fixed as you have mentioned.
Regards
Jatan
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote: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...

And I thought you figured out how to do this??
And you don't need to create another topic for the same question.. You will get same answers..
Dinesh Kumar Ramakrishnan
Ranch Hand

Joined: Aug 07, 2012
Posts: 47

deepika deepi wrote: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...


By using FileReader read the content of file line by line and compare the your string with file content
try the following code


“If you’re good at something, never do it for free.” —The Joker
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
R. Jain wrote:
deepika deepi wrote: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...

And I thought you figured out how to do this??
And you don't need to create another topic for the same question.. You will get same answers..


ya... i was not actually double post yaar... the time i give on net the post , it was not posted. So i tried to post it in new one.. Sorry for that....
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote:
R. Jain wrote:
deepika deepi wrote: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...

And I thought you figured out how to do this??
And you don't need to create another topic for the same question.. You will get same answers..


ya... i was not actually double post yaar... the time i give on net the post , it was not posted. So i tried to post it in new one.. Sorry for that....


NoNeedToSaySorry .. It happens..
Your problem is quite similar to the previous one.. Instead of using ArrayList to store the words, you can rather use HashMap to store them as Key-Value Pair..
See HashMap..
deepika deepi
Ranch Hand

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

I have a input like this

001 001001 located,reserve Rule1.0

001 001001 reserve,biosphere Rule1.1


I want to split the the third word with , and get the split output like this
001 001001 located Rule1.0
001 001001 reserve Rule1.0
001 001001 reserve Rule1.1
001 001001 biosphere Rule1.1


the input can also be

001 001001 located,reserve,biosphere,serve Rule1.2
and so on...

I can split using the delimiter. But the problem is i have many words with comma so i would like to know any other alternate solution existing for this problem ?
if i use for the words are initialised inside and cannot be used outside the for loop. so what can be done to solve this ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

deepika deepi wrote:the input can also be
001 001001 located,reserve,biosphere,serve Rule1.2
and so on...

Yeah, and it's the "and so on" part that I don't follow.

However, hazarding a guess here, I'd say that you have columns separated by TABs (and just in case anybody else didn't see - they are TABs), with the 3rd column being a comma-delimited set of words.

I can split using the delimiter. But the problem is i have many words with comma so i would like to know any other alternate solution existing for this problem ?
if i use for the words are initialised inside and cannot be used outside the for loop.

What for loop? I don't see any.

However, if what I said above is correct, the solution is simple:
1. Split the line using TAB as a delimiter.
2. Split the 3rd element of the result of (1) using comma as a delimiter.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3275
Can you use regex?


500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski wrote:

What for loop? I don't see any.

However, if what I said above is correct, the solution is simple:
1. Split the line using TAB as a delimiter.
2. Split the 3rd element of the result of (1) using comma as a delimiter.

Winston


Yor telling to use like this

String[] input = sentence.split("\t");

String integer = input[0];
String integer1 = input[1];
String keyword = input[2];

String rule = input[3];
String[] keywordsplit = keyword.split(",");
String keywordsplit0 = keywordsplit[0];
String keywordsplit1 =keywordsplit[1];
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote:
Yor telling to use like this

String[] input = sentence.split("\t");

String integer = input[0];
String integer1 = input[1];
String keyword = input[2];

String rule = input[3];
String[] keywordsplit = keyword.split(",");
String integer = keywordsplit[0];
String integer1 =keywordsplit[1];


Yes.. you did it right.. Check the duplicate names in variable declaration..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

deepika deepi wrote:Yor telling to use like this

Looks right to me, IF what I guessed about the format was correct.

Except for those last two lines. Why are you re-assigning integer and integer1 again?
keywordsplit[0] and keywordsplit[1] will be words, won't they?

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski wrote:
Looks right to me, IF what I guessed about the format was correct.

Winston


ya. thank you. i tried it earlier. But the expected and the actual output is not the same.

i am getting the output like this :

line output:located Rule1.0
line output:serve Rule1.1



code used is:

the expected output is :
line output:located Rule1.0
line output:serve Rule1.0


so i had an doubt how to use that. now the problem i have got and help me to solve this

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski wrote:
deepika deepi wrote:Yor telling to use like this

Looks right to me, IF what I guessed about the format was correct.

Except for those last two lines. Why are you re-assigning integer and integer1 again?
keywordsplit[0] and keywordsplit[1] will be words, won't they?

Winston


I use integer and integer1 because i have two different types of Integers so i used it
ya they are keywordsplit[0] and keywordsplit[1] will be words
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

deepika deepi wrote:ya they are keywordsplit[0] and keywordsplit[1] will be words

Right, so don't put them in fields called 'integer...'. It's confusing.

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski wrote:
deepika deepi wrote:ya they are keywordsplit[0] and keywordsplit[1] will be words

Right, so don't put them in fields called 'integer...'. It's confusing.

Winston


ya i have corrected it how to get the expected output from the actual output. Help me
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
R. Jain wrote:
deepika deepi wrote:
Yor telling to use like this

String[] input = sentence.split("\t");

String integer = input[0];
String integer1 = input[1];
String keyword = input[2];

String rule = input[3];
String[] keywordsplit = keyword.split(",");
String integer = keywordsplit[0];
String integer1 =keywordsplit[1];


Yes.. you did it right.. Check the duplicate names in variable declaration..


for what purpose i need to check the duplicate names. i don't understand.
can you be bit clear.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

deepika deepi wrote:how to get the expected output from the actual output. Help me

What expected output? You haven't explained what you want.

And please EaseUp (←click). We're all volunteers here.

The minute you come up with a properly formulated question, with detailed examples of exactly what you want to see from specific examples of input, is the minute you're likely to start getting answers that satisfy you.

BUT NOT BEFORE.

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
the expected output is:
line output:located Rule1.0
line output:serve Rule1.0


the actual input is :
line output:located Rule1.0
line output:serve Rule1.1


code is:


input file is:
001 001001 located,reserve Rule1.0
001 001001 reserve,biosphere Rule1.1


i hope i am making it easier now. I have given all the datas. Help me.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

deepika deepi wrote:code is:

No it isn't; because that code could not possibly produce the output you say it does.

i hope i am making it easier now. I have given all the datas. Help me.

As I said before, EaseUp. Simply repeating "Help me" isn't going to get the job done any quicker.

Read the HowToAskQuestionsOnJavaRanch page; and also this one and this one.

And when you've done that, come back and explain your problem, NOT your solution.

Winston
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote:
code is:


You are using a variable(rule) before declaring it ---> Will not compile..

deepika deepi wrote:the expected output is:
line output:located Rule1.0
line output:serve Rule1.0


the actual input is :
line output:located Rule1.0
line output:serve Rule1.1


Your expected output contains : Rule1.0 on both the lines.. See the last word of the two lines in your output..
Whereas, your input has: Rule1.0 & Rule1.1 on the two lines..
You need to handle that.. If you want Rule1.0 on both the lines, you can store it once when you get it from the first line (And then use it..)


This code will just assign the 4th word of each line to the variable rule, for each iteration (Which you don't want).. I suppose from your expected output..
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski wrote:
deepika deepi wrote:code is:

No it isn't; because that code could not possibly produce the output you say it does.

i hope i am making it easier now. I have given all the datas. Help me.

As I said before, EaseUp. Simply repeating "Help me" isn't going to get the job done any quicker.

Read the HowToAskQuestionsOnJavaRanch page; and also this one and this one.

And when you've done that, come back and explain your problem, NOT your solution.

Winston


my problem is

the word serve and located i have the rule as Rule1.0 according to the input file.

the word is splitted using , then the splitted rule is matched with the rule.

the word serve is splitted with and matched against the rule. the rule is supposed to be Rule1.0 but it is going to the next rule number. i don't want to match the word serve with the next rule . i want it to be matching with the input rule file. I have explained to my best. If you could see the actual output also you will be able to see that. I have tried my level best to explain the problem
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote:input file is:
001 001001 located,reserve Rule1.0
001 001001 reserve,biosphere Rule1.1

For the above input file: -

deepika deepi wrote:

What will be size of array generated when you use the above split code??

deepika deepi wrote:

Here you are considering only the first element of the array obtained after splitting.. keywordsplit[0]
What about the next element--> keywordsplit[1]... What will it contain??
It is this element you want..

You need to iterated over the array here to get the output what you expect..
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
R. Jain wrote:
What will be size of array generated when you use the above split code??

size of the array is 15.

this is the input having maximun number of array


001 001001 located,reserve Rule1.0
001 001001 serve,biosphere Rule1.1
002 002002 star Rule2.0
002 002002 chief,star,main,popular,tourist,famous,attractions,attraction,important,destination,renowned,renown,popularity,fascinating,interesting,entertaining Rule 2.1


keywordsplit[0] contain output: located serve chief
keywordsplit[1] contain output: reserve biosphere star
keywordsplit[2] contain output is not displayed

It gives am error:
java.lang.ArrayIndexOutOfBoundsException: 2
at A.method1(Ruleselection.java:32)
at Ruleselection.main(Ruleselection.java:92)


R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote:size of the array is 15.

In which case, you are not getting your code..
Try to print the array obtained as you split your keyword.. Also print its length..

deepika deepi wrote:
keywordsplit[0] contain output: located serve chief
keywordsplit[1] contain output: reserve biosphere star
keywordsplit[2] contain output is not displayed

No they don't.. Try printing array and see what you get..
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
R. Jain wrote:
deepika deepi wrote:size of the array is 15.

In which case, you are not getting your code..
Try to print the array obtained as you split your keyword.. Also print its length..

deepika deepi wrote:
keywordsplit[0] contain output: located serve chief
keywordsplit[1] contain output: reserve biosphere star
keywordsplit[2] contain output is not displayed

No they don't.. Try printing array and see what you get..


Ya i got the result using for loop
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: match the word from text file