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..10546217 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,34 @@ 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("Error reading file: " + 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); } -} +} \ No newline at end of file