Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mastersec #1444

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/basesyntax/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WWW? Four-bedroom farmhouse in the countryside. Wave! All of the four double bedrooms are en suite.
34 changes: 30 additions & 4 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -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<String> readAndFilterWords(String filePath) {
List<String> 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<String> filteredWords = readAndFilterWords(filePath);
System.out.println(filteredWords);
}
}
}
Loading