Advertisement
jaVer404

level19.lesson05.task02_(done)_6 attempts

Feb 19th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. package com.javarush.test.level19.lesson05.task02;
  2.  
  3. /* Считаем слово
  4. Считать с консоли имя файла.
  5. Файл содержит слова, разделенные знаками препинания.
  6. Вывести в консоль количество слов "world", которые встречаются в файле.
  7. Закрыть потоки. Не использовать try-with-resources
  8.  
  9. //s = s.replaceAll("[^a-zA-Z0-9\\s]", "");
  10. str = "Hello I'm your String";
  11. String[] splited = str.split(" ");
  12. */
  13.  
  14. import java.io.*;
  15. public class Solution {
  16.     public static void main(String[] args) throws IOException{
  17.         BufferedReader nameReader = new BufferedReader(new InputStreamReader(System.in));
  18.         String fileName = nameReader.readLine();
  19.         nameReader.close();
  20.         String specWord = "world";
  21.         System.out.println(countWord(fileName,specWord));
  22.     }
  23.     public static int countWord(String file, String word) {
  24.         BufferedReader readeFile = null;
  25.         int counter = 0;
  26.         try
  27.         {
  28.             readeFile = new BufferedReader(new FileReader(file));
  29.             String tmp = "";
  30.             while ((tmp=readeFile.readLine())!=null){
  31.                 tmp=tmp.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
  32.                 String[]strings = tmp.split(" ");
  33.                 for (String s : strings) {
  34.                     if (s.equals(word)) {
  35.                         counter++;
  36.                     }
  37.                 }
  38.             }
  39.             readeFile.close();
  40.         }
  41.         catch (IOException e)
  42.         {}
  43.         finally
  44.         {
  45.             if (readeFile!=null) {
  46.                 try
  47.                 {
  48.                     readeFile.close();
  49.                 }
  50.                 catch (IOException e) {}
  51.             }
  52.         }
  53.         return counter;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement