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
Showing
1 changed file
with
37 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,43 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class FileWork { | ||
private static final String SPECIFIED_CHARACTER = "w"; | ||
private static final String WORD_SEPARATOR = " "; | ||
|
||
public String[] readFromFile(String fileName) { | ||
//write your code here | ||
return null; | ||
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) { | ||
String read = bufferedReader.readLine(); | ||
List<String> stringList = new ArrayList<>(); | ||
while (read != null) { | ||
read = read.toLowerCase(); | ||
String[] words = read.split(WORD_SEPARATOR); | ||
for (String word : words) { | ||
if (word.startsWith(SPECIFIED_CHARACTER)) { | ||
stringList.add(word.replaceAll("\\p{Punct}", "")); | ||
} | ||
} | ||
Collections.sort(stringList); | ||
read = bufferedReader.readLine(); | ||
} | ||
String[] stringArray = new String[stringList.size()]; | ||
|
||
for (int i = 0; i < stringArray.length; i++) { | ||
stringArray[i] = stringList.get(i); | ||
} | ||
if (stringList.isEmpty()) { | ||
return new String[]{}; | ||
} else { | ||
return stringArray; | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("File not found exception"); | ||
} | ||
} | ||
} |