From 71c830d4982ffde6d5145963cd2a4316fd6183b3 Mon Sep 17 00:00:00 2001 From: Weronika Rzekiecka Date: Fri, 7 Feb 2025 11:47:08 +0100 Subject: [PATCH 1/4] Filework --- src/core/basesyntax/words.txt | 1 + src/main/java/core/basesyntax/FileWork.java | 33 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/core/basesyntax/words.txt diff --git a/src/core/basesyntax/words.txt b/src/core/basesyntax/words.txt new file mode 100644 index 00000000..2be97a3c --- /dev/null +++ b/src/core/basesyntax/words.txt @@ -0,0 +1 @@ +WWW? Four-bedroom farmhouse in the countryside. Wave! All of the four double bedrooms are en suite. \ No newline at end of file diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..3621ae22 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,35 @@ package core.basesyntax; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + public class FileWork { - public String[] readFromFile(String fileName) { - //write your code here - return null; + public static List readAndFilterWords(String filePath) { + List result = new ArrayList<>(); + try { + String content = new String(Files.readAllBytes(Paths.get(filePath))); + String[] words = content.split("\\W+"); + + for (String word : words) { + if (word.toLowerCase().startsWith("w")) { + result.add(word.toLowerCase()); + } + } + Collections.sort(result); + } catch (IOException e) { + System.out.println("Błąd odczytu pliku: " + e.getMessage()); + } + return result; + } + + public static void main(String[] args) { + String filePath = "src/core/basesyntax/words.txt"; + List filteredWords = readAndFilterWords(filePath); + System.out.println(filteredWords); } } + From d3b17fa2e41b6f2d76c6ad250a2362984aed37c5 Mon Sep 17 00:00:00 2001 From: Weronika Rzekiecka Date: Wed, 12 Feb 2025 11:29:17 +0100 Subject: [PATCH 2/4] poprawione zadanie --- src/main/java/core/basesyntax/FileWork.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index 3621ae22..8156bcc0 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -31,5 +31,4 @@ public static void main(String[] args) { List filteredWords = readAndFilterWords(filePath); System.out.println(filteredWords); } -} - +} \ No newline at end of file From 1a520f2a4d486a4a9b40afb2670d674f3c11de40 Mon Sep 17 00:00:00 2001 From: Weronika Rzekiecka Date: Wed, 12 Feb 2025 11:41:58 +0100 Subject: [PATCH 3/4] .. --- src/main/java/core/basesyntax/FileWork.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index 8156bcc0..b7f75cc5 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -31,4 +31,6 @@ public static void main(String[] args) { List filteredWords = readAndFilterWords(filePath); System.out.println(filteredWords); } + + } \ No newline at end of file From 268dd06cd4f077cc56c8c77b15e5d7e761aeef3a Mon Sep 17 00:00:00 2001 From: Weronika Rzekiecka Date: Wed, 12 Feb 2025 12:23:22 +0100 Subject: [PATCH 4/4] Poprawiony styl zgodnie z Checkstyle --- src/main/java/core/basesyntax/FileWork.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index b7f75cc5..10546217 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -21,7 +21,7 @@ public static List readAndFilterWords(String filePath) { } Collections.sort(result); } catch (IOException e) { - System.out.println("Błąd odczytu pliku: " + e.getMessage()); + System.out.println("Error reading file: " + e.getMessage()); } return result; } @@ -31,6 +31,4 @@ public static void main(String[] args) { List filteredWords = readAndFilterWords(filePath); System.out.println(filteredWords); } - - } \ No newline at end of file