Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlavrinenko committed Dec 30, 2024
1 parent 160910a commit 8a589c5
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
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 int FIRST_LETTER_INDEX = 0;
private static final char FIRST_LETTER = 'w';
private static final int INITIAL_COUNTER_VALUE = 0;

public String[] readFromFile(String fileName) {
//write your code here
return null;
String fileInfo;

try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
StringBuilder stringBuilder = new StringBuilder();
String value = reader.readLine();

while (value != null) {
stringBuilder.append(value).append(System.lineSeparator());
value = reader.readLine();
}

fileInfo = stringBuilder.toString().toLowerCase().replaceAll("\\p{Punct}", "");
} catch (IOException e) {
throw new RuntimeException("Can't read file", e);
}

String[] words = fileInfo.split("\\s+");

int wordsCounter = INITIAL_COUNTER_VALUE;

for (String word : words) {
if (word != null && !word.isEmpty()
&& word.toCharArray()[FIRST_LETTER_INDEX] == FIRST_LETTER) {
wordsCounter++;
}
}

String[] result = new String[wordsCounter];

wordsCounter = INITIAL_COUNTER_VALUE;

for (String word : words) {
if (word != null && !word.isEmpty()
&& word.toCharArray()[FIRST_LETTER_INDEX] == FIRST_LETTER) {
result[wordsCounter] = word;
wordsCounter++;
}
}

Arrays.sort(result);

return result;
}
}

0 comments on commit 8a589c5

Please sign in to comment.