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
- //s = s.replaceAll("[^a-zA-Z0-9\\s]", "");
- str = "Hello I'm your String";
- String[] splited = str.split(" ");
- */
- import java.io.*;
- public class Solution {
- public static void main(String[] args) throws IOException{
- BufferedReader nameReader = new BufferedReader(new InputStreamReader(System.in));
- String fileName = nameReader.readLine();
- nameReader.close();
- String specWord = "world";
- System.out.println(countWord(fileName,specWord));
- }
- public static int countWord(String file, String word) {
- BufferedReader readeFile = null;
- int counter = 0;
- try
- {
- readeFile = new BufferedReader(new FileReader(file));
- String tmp = "";
- while ((tmp=readeFile.readLine())!=null){
- tmp=tmp.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
- String[]strings = tmp.split(" ");
- for (String s : strings) {
- if (s.equals(word)) {
- counter++;
- }
- }
- }
- readeFile.close();
- }
- catch (IOException e)
- {}
- finally
- {
- if (readeFile!=null) {
- try
- {
- readeFile.close();
- }
- catch (IOException e) {}
- }
- }
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement