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

release filter out only the words starting with 'w' #1097

Closed
wants to merge 2 commits into from
Closed
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.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class FileWork {
public String[] readFromFile(String fileName) {
//write your code here
return null;
if (fileName.isEmpty() || fileName == "") {
return new String[0];
}

StringBuilder stringBuilder = new StringBuilder();
File file = new File(fileName);
String value;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
value = reader.readLine();
while (value != null) {
stringBuilder.append(value).append(System.lineSeparator());
value = reader.readLine();
}
} catch (IOException e) {
throw new RuntimeException("Can`t read file", e);
}
if (stringBuilder.length() == 0) {
return new String[0];
}
String[] strings = stringBuilder.toString().toLowerCase().split("\\W+");
stringBuilder.setLength(0);
for (String str : strings) {
if (str.charAt(0) == 'w') {
stringBuilder.append(str).append(System.lineSeparator());
}
}
if (stringBuilder.length() > 0) {
String[] result = stringBuilder.toString().split(System.lineSeparator());
Arrays.sort(result);
return result;
}
return new String[0];
}
}
Loading