更新时间:2022-12-10 10:07:56
lineByLine
指向整个你文件的内容。这就是你得到 [4,4,4,4,4,4]
的原因。您需要将每一行存储在另一个变量行
中,然后使用 tagFinder.find(line)
。
最终代码将如下所示
lineByLine
points 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 variable line
and then use tagFinder.find(line)
.
Final code will look like this
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.group() returns the input subsequence matched by the previous match.
Matcher tagMatcher = tagFinder.matcher(matcher.group());
while(tagMatcher.find())
{
score++;
}
scoreTracker.add(score);
score = 0;
}
}