скрин
![]()
package com.javarush.task.task19.task1907;
/*
Считаем слово
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
FileReader fileReader = new FileReader(fileName);
BufferedReader bFileReader = new BufferedReader(fileReader);
String nextLine = "", world = "world", separators = ".,:;-?!\"() ";
int count = 0, offset;
while ((nextLine = bFileReader.readLine()) != null) {
while ((offset = nextLine.indexOf(world)) != -1 && !nextLine.isEmpty()) {
//when world is included into separators ,,,,world,,,,
if (offset > 0 && nextLine.length() - offset > world.length()) {
if (separators.contains(nextLine.substring(offset - 1, offset)) && separators.contains(nextLine.substring(offset + world.length(), offset + world.length()+1))) {
++count;
}
nextLine = nextLine.substring(offset + world.length()+1);
} else {
if (offset == 0) {
if (nextLine.length() == 5) {// строка из одного слова
++count;
nextLine = "";
}
else {
if (nextLine.length() > 5 && separators.contains(nextLine.substring(world.length(), world.length() + 1))) {
++count;
}
nextLine = nextLine.substring(world.length()+1);
}
}
}
}
}
fileReader.close();
bFileReader.close();
System.out.print(count);
}
}
содержимое файла
1234567890123456789123456789aFFAvasdfB@df_12
The world, is mine -,!world-thebest-world,world everI see this worldworld wherever I've been over the world!
world world
world worldW
world
WorlD
wkorld