Skip to content

Commit

Permalink
added BufferedReader
Browse files Browse the repository at this point in the history
added letterForSorting
  • Loading branch information
MaximGolyak committed Dec 14, 2024
1 parent 160910a commit c90b1b2
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileWork {
private static final String letterForSorting = "w";

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

try {
BufferedReader bufferedReader = new BufferedReader(new StringReader(fileName));
StringBuilder stringBuilder = new StringBuilder();
String content = bufferedReader.readLine();

while (content != null) {
stringBuilder.append(content);
content = bufferedReader.readLine();
}
String inputText = stringBuilder.toString().toLowerCase();

Pattern pattern = Pattern.compile("[^a-z]+");
Matcher matcher = pattern.matcher(inputText);
String result = matcher.replaceAll(" ");

String[] wordsArray = result.split(" ");
int count = 0;

for (String word : wordsArray) {
if (word.startsWith(letterForSorting)) {
count++;
}
}

String[] filteredWords = new String[count];
int index = 0;

for (String word : wordsArray) {
if (word.startsWith("w")) {
filteredWords[index++] = word;
}
}
Arrays.sort(filteredWords);
System.out.println(Arrays.toString(filteredWords));
return new String[]{Arrays.toString(filteredWords)};
} catch (Exception e) {
return new String[0];
}
}

}

0 comments on commit c90b1b2

Please sign in to comment.