Here’s the code that I’ve worked upon:
while ((lineContents = tempFileReader.readLine()) != null)
{
String lineByLine = lineContents.replaceAll("/\\.", System.getProperty("line.separator")); //for matching /. and replacing it by new line
changer.write(lineByLine);
Pattern pattern = Pattern.compile("\\r?\\n"); //Find new line
Matcher matcher = pattern.matcher(lineByLine);
while(matcher.find())
{
Pattern tagFinder = Pattern.compile("word"); //Finding the word required
Matcher tagMatcher = tagFinder.matcher(lineByLine);
while(tagMatcher.find())
{
score++;
}
scoreTracker.add(score);
score = 0;
}
}
My sample input contains 6 lines, with occurences of word being [0,1,0,3,0,0]
So when I print scoreTracker (which is an ArrayList) I want the above output.
But instead, I get [4,4,4,4,4,4] which it the total occurence of the word, but not line by line.
Kindly help.
lineByLinepoints to the entire contents of your file. That is the reason you are getting[4,4,4,4,4,4]. You need to store each line in another variablelineand then usetagFinder.find(line).Final code will look like this