From 5145a2f117dae04f558b30c67310a8c227b0e82d Mon Sep 17 00:00:00 2001 From: YarikNarozhnyi Date: Sun, 24 Nov 2024 11:45:46 +0000 Subject: [PATCH 1/6] 3874834 --- src/main/java/core/basesyntax/FileWork.java | 28 +++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..81831b14 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,32 @@ package core.basesyntax; +import java.util.ArrayList; +import java.util.List; + public class FileWork { public String[] readFromFile(String fileName) { - //write your code here - return null; + String[] split = fileName.split("\\W+"); + + int count = 0; + for (String word : split) { + if (word.toLowerCase().startsWith("w")) { + count++; + } + } + + if (count == 0) { + return new String[0]; + } + + String[] filteredWords = new String[count]; + int index = 0; + for (String word : split) { + if (word.toLowerCase().startsWith("w")) { + filteredWords[index] = word.toLowerCase(); + index++; + } + } + + return filteredWords; } } From 8ed5d79ac16d6ffdcac474b7a1fe0d9888c71ba9 Mon Sep 17 00:00:00 2001 From: YarikNarozhnyi Date: Sun, 24 Nov 2024 12:00:59 +0000 Subject: [PATCH 2/6] 84848 --- src/main/java/core/basesyntax/FileWork.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index 81831b14..ce9a0079 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,5 @@ package core.basesyntax; -import java.util.ArrayList; -import java.util.List; - public class FileWork { public String[] readFromFile(String fileName) { String[] split = fileName.split("\\W+"); From 18a04ff696fbc7017c663236501fd9ed668933f9 Mon Sep 17 00:00:00 2001 From: YarikNarozhnyi Date: Sun, 24 Nov 2024 12:13:29 +0000 Subject: [PATCH 3/6] first --- src/main/java/core/basesyntax/FileWork.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ce9a0079..5d51a418 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -23,7 +23,6 @@ public String[] readFromFile(String fileName) { index++; } } - return filteredWords; } } From d23a48329a80510f104617f1744145bb2263d340 Mon Sep 17 00:00:00 2001 From: YarikNarozhnyi Date: Sun, 24 Nov 2024 12:30:46 +0000 Subject: [PATCH 4/6] second --- src/main/java/core/basesyntax/FileWork.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index 5d51a418..42cc4b8a 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -17,12 +17,12 @@ public String[] readFromFile(String fileName) { String[] filteredWords = new String[count]; int index = 0; - for (String word : split) { + for (String word : split) { if (word.toLowerCase().startsWith("w")) { filteredWords[index] = word.toLowerCase(); index++; } - } + } return filteredWords; } } From a691b4233e5654998326463658965e6bb1770e53 Mon Sep 17 00:00:00 2001 From: YarikNarozhnyi Date: Sun, 24 Nov 2024 14:11:23 +0000 Subject: [PATCH 5/6] final --- src/main/java/core/basesyntax/FileWork.java | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index 42cc4b8a..b8bf452c 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,11 +1,28 @@ package core.basesyntax; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + public class FileWork { public String[] readFromFile(String fileName) { - String[] split = fileName.split("\\W+"); - int count = 0; - for (String word : split) { + StringBuilder stringBuilder = new StringBuilder(); + + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) { + String line = bufferedReader.readLine(); + while (line != null) { + stringBuilder.append(line).append(" "); + line = bufferedReader.readLine(); + } + } catch (IOException e) { + throw new RuntimeException("Can't read a file", e); + } + + String[] split = stringBuilder.toString().split("\\W+"); + + int count = 0; + for (String word : split) { if (word.toLowerCase().startsWith("w")) { count++; } From 327ae109f51d377d0d811571dd6f5c7615e1531e Mon Sep 17 00:00:00 2001 From: YarikNarozhnyi Date: Sun, 24 Nov 2024 14:23:06 +0000 Subject: [PATCH 6/6] ffinali --- src/main/java/core/basesyntax/FileWork.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index b8bf452c..0c26e8c9 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -6,7 +6,6 @@ public class FileWork { public String[] readFromFile(String fileName) { - StringBuilder stringBuilder = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) { @@ -15,14 +14,14 @@ public String[] readFromFile(String fileName) { stringBuilder.append(line).append(" "); line = bufferedReader.readLine(); } - } catch (IOException e) { + } catch (IOException e) { throw new RuntimeException("Can't read a file", e); } - String[] split = stringBuilder.toString().split("\\W+"); + String[] split = stringBuilder.toString().split("\\W+"); - int count = 0; - for (String word : split) { + int count = 0; + for (String word : split) { if (word.toLowerCase().startsWith("w")) { count++; }