На тест: The magicworld. In there may be fantaskis things. Notreadlworld. First world.
Выдаёт 1
Как я понял, сканер прилепляет знаки препинания к концу слова, поэтому пришлось и над этим чуть посидеть
/*
Выделяем числа
*/
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Scanner file1 = new Scanner(new FileReader(reader.readLine()));
reader.close();
int countOfWorld = 0;
while (file1.hasNext()) {
String word = file1.next();
word = checkIfEndOfWordIsWithoutPunctuationMark(word);
if (word.equals("world")) {
countOfWorld++;
}
}
System.out.println(countOfWorld);
file1.close();
}
private static String checkIfEndOfWordIsWithoutPunctuationMark(String word) {
int lastLetterIndex = word.length() - 1;
char lastLetter = word.charAt(lastLetterIndex);
if (!Character.isLetterOrDigit(lastLetter)) {
word = word.substring(0, lastLetterIndex);
}
return word;
}
}