Skip to content

Commit

Permalink
Complete task according to the conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
htsarenko committed Sep 18, 2024
1 parent 160910a commit 8e9e115
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
package core.basesyntax;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

public class FileWork {
public String[] readFromFile(String fileName) {
//write your code here
return null;
try {
String content = Files.readString(Path.of(fileName));

String[] words = content.split("\\W+");

//convert to lowerCase
for (int i = 0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}

int countOfWords = 0;
for (String word : words) {
if (word.startsWith("w")) {
countOfWords++;
}
}

String[] filteredWords = new String[countOfWords];

int indexArray = 0;
for (String word : words) {
if (word.startsWith("w")) {
filteredWords[indexArray++] = word;
}
}

Arrays.sort(filteredWords);

return filteredWords;

} catch (IOException e) {
throw new RuntimeException("Can't read information from file");
}
}
}

0 comments on commit 8e9e115

Please sign in to comment.