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

complited task #1345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 46 additions & 1 deletion src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
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 {
private static final int INDEX_OF_FIRST_CHAR = 0;
private static final int STARTING_INDEX = 0;

public String[] readFromFile(String fileName) {
//write your code here
return null;
File file = new File(fileName);
String[] wordsStartingWithW;

try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
StringBuilder stringBuilder = new StringBuilder();
String value = bufferedReader.readLine();

while (value != null) {
stringBuilder.append(value).append(" ");
value = bufferedReader.readLine();
}
String[] words = stringBuilder.toString().split("\\W+");

int counter = 0;
for (String word : words) {
if (!word.isEmpty() && (word.charAt(0) == 'w' || word.charAt(0) == 'W')) {
counter++;
}
}
wordsStartingWithW = new String[counter];

int index = STARTING_INDEX;
for (String word : words) {
if (!word.isEmpty() && (word.charAt(INDEX_OF_FIRST_CHAR) == 'w'
|| word.charAt(0) == 'W')) {
wordsStartingWithW[index] = word.toLowerCase();
index++;
}
}

Arrays.sort(wordsStartingWithW);

} catch (IOException e) {
throw new RuntimeException("Error" + e.getMessage(), e);
}

return wordsStartingWithW;
}
}
Loading