Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level19.lesson05.task02;
- /* Считаем слово
- Считать с консоли имя файла.
- Файл содержит слова, разделенные знаками препинания.
- Вывести в консоль количество слов "world", которые встречаются в файле.
- Закрыть потоки. Не использовать try-with-resources
- */
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String inFile = reader.readLine();
- reader.close();
- BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), StandardCharsets.UTF_8));
- String inData;
- int count=0;
- while ((inData=fileReader.readLine())!=null) {
- String[] tmp = inData.split("[^\\w%]");
- for (int i = 0; i < tmp.length; i++) {
- if (tmp[i].matches("\\bworld\\b")) {
- count++;
- }
- }
- }
- System.out.println(count);
- fileReader.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement