generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
160910a
commit 8a589c5
Showing
1 changed file
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |