Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. package com.javarush.test.level19.lesson05.task02;
  2.  
  3. /* Считаем слово
  4. Считать с консоли имя файла.
  5. Файл содержит слова, разделенные знаками препинания.
  6. Вывести в консоль количество слов "world", которые встречаются в файле.
  7. Закрыть потоки. Не использовать try-with-resources
  8. */
  9.  
  10. import java.io.*;
  11. import java.nio.charset.StandardCharsets;
  12.  
  13. public class Solution {
  14. public static void main(String[] args) throws IOException {
  15. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  16. String inFile = reader.readLine();
  17. reader.close();
  18.  
  19. BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), StandardCharsets.UTF_8));
  20. String inData;
  21. int count=0;
  22. while ((inData=fileReader.readLine())!=null) {
  23. String[] tmp = inData.split("[^\\w%]");
  24. for (int i = 0; i < tmp.length; i++) {
  25. if (tmp[i].matches("\\bworld\\b")) {
  26. count++;
  27. }
  28. }
  29. }
  30. System.out.println(count);
  31. fileReader.close();
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement