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

filter file the words starting with "w" and sorted all words be lower… #1034

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 39 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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;
List<String> filteredWords = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while (true) {
try {
if ((line = reader.readLine()) == null) {
break;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
String[] words = line.split("\\W+");
for (String word : words) {
word = word.replaceAll("[^a-zA-Z]", "").toLowerCase();
if (word.startsWith("w")) {
filteredWords.add(word);
}
}
}
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}

Collections.sort(filteredWords);

String[] result = new String[filteredWords.size()];
return filteredWords.toArray(result);
}

}

17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax;

import java.io.File;

public class Main {
public static void main(String[] args) {
File file = new File("test.txt");

FileWork wordFilter = new FileWork();
String fileName = file.getPath();
String[] filteredWords = wordFilter.readFromFile(fileName);

for (String word : filteredWords) {
System.out.println(word);
}
}
}
2 changes: 2 additions & 0 deletions test1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

"WWW? Four-bedroom farmhouse in the countryside. Wave! All of the four double bedrooms are en suite."
Loading