From a9926a6605f3d07facb71024d85a39d6113d2d1b Mon Sep 17 00:00:00 2001 From: Ihor Tkachuk Date: Mon, 16 Sep 2024 22:56:13 +0300 Subject: [PATCH] added solution --- src/main/java/core/basesyntax/FileWork.java | 62 ++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..b5a8e879 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,66 @@ package core.basesyntax; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Arrays; + public class FileWork { public String[] readFromFile(String fileName) { - //write your code here - return null; + + File file = new File(fileName); + BufferedReader bufferedReader; + try { + bufferedReader = new BufferedReader(new FileReader(file)); + } catch (FileNotFoundException e) { + throw new RuntimeException("File not found ",e); + } + StringBuilder stringBuilder = new StringBuilder(); + int value; + try { + value = bufferedReader.read(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + if (value == -1) { + return new String[0]; + } + try { + while (value != -1) { + stringBuilder.append((char) value); + value = bufferedReader.read(); + } + + } catch (FileNotFoundException e) { + System.out.println("File not found " + e); + } catch (IOException e) { + System.out.println("Can't read file " + e); + } + + String[] textArray = stringBuilder.toString().replaceAll("\n", " ").toLowerCase() + .split(" "); + int counterW = 0; + + for (String word: textArray) { + + if (word.charAt(0) == 'w') { + counterW++; + } + } + String[] wordArray = new String[counterW]; + int indexWordArray = -1; + + for (String word: textArray) { + if (word.charAt(0) == 'w') { + indexWordArray++; + wordArray[indexWordArray] = word.replaceAll("[^a-zA-Z]", ""); + } + } + Arrays.sort(wordArray); + return wordArray; } } +