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

Add files via upload #1016

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
35 changes: 33 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class FileWork {
private static final String SEPARATOR_OF_WORDS = " ";

public String[] readFromFile(String fileName) {
//write your code here
return null;
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
int value = reader.read();
StringBuilder builder = new StringBuilder();
while (value != -1) {
builder.append((char) value);
value = reader.read();
}
String[] words = builder.toString().toLowerCase().split("\\W+");
StringBuilder resultBuilder = new StringBuilder();
for (String word : words) {
if (word.charAt(0) == 'w') {
resultBuilder.append(word).append(SEPARATOR_OF_WORDS);
}
}
if (resultBuilder.toString().isEmpty()) {
return new String[0];
}
String[] appropriateWords = resultBuilder.toString().trim().split(SEPARATOR_OF_WORDS);
Arrays.sort(appropriateWords);
return appropriateWords;
} catch (IOException e) {
throw new RuntimeException("Can't read file " + e);
} catch (StringIndexOutOfBoundsException e) {
return new String[0];
}
}
}
Loading