Skip to content

Instantly share code, notes, and snippets.

@AIRAT1
Created September 29, 2014 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AIRAT1/75e241a71e1c792e2b63 to your computer and use it in GitHub Desktop.
Save AIRAT1/75e241a71e1c792e2b63 to your computer and use it in GitHub Desktop.
package com.javarush.test.level19.lesson05.task02;
/* Считаем слово
Считать с консоли имя файла.
Вывести в консоль количество слов "world", которые встречаются в файле.
Закрыть поток ввода.
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Scanner scanner = new Scanner(new FileReader(reader.readLine()));
ArrayList<String> list = new ArrayList<>();
int count = 0;
while (scanner.hasNext()) {
list.add(scanner.next());
}
char[] chars = list.toString().toCharArray();
//System.out.println(chars);
for (int i = 0; i < chars.length - 5; i++) {
String s = "";
for (int j = i; j < i+5; j++) {
s += chars[j];
}
//System.out.println(s);
if (s.toLowerCase().equals("world")) count++;
}
System.out.println(count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment