Skip to content

Commit

Permalink
created the sorting logic and decompressed the file
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Sukhanko committed Oct 4, 2024
1 parent 160910a commit 6d6d779
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
package core.basesyntax;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileWork {
public String[] readFromFile(String fileName) {
//write your code here
return null;
String regex = "\\b[wW]\\w*\\b";

try {
String words = new String(Files.readAllBytes(Paths.get(fileName)));

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(words);

List<String> filteredWords = new ArrayList<>();
while (matcher.find()) {
filteredWords.add(matcher.group().toLowerCase(Locale.ROOT));
}

if (filteredWords.isEmpty()) {
return new String[0];
}

Collections.sort(filteredWords);

return filteredWords.toArray(new String[0]);

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

0 comments on commit 6d6d779

Please sign in to comment.