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 60e60e4
Showing
1 changed file
with
33 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,39 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Arrays; | ||
|
||
public class FileWork { | ||
public String[] readFromFile(String fileName) { | ||
//write your code here | ||
return null; | ||
File file = new File(fileName); | ||
String result = ""; | ||
try { | ||
result = Files.readString(file.toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't find the file"); | ||
} | ||
|
||
String[] fromFile = result.split("\\W+"); | ||
int length = 0; | ||
for (int i = 0; i < fromFile.length; i++) { | ||
if (fromFile[i].toLowerCase().startsWith("w")) { | ||
length++; | ||
} | ||
} | ||
|
||
int index = 0; | ||
String[] resultArray = new String[length]; | ||
for (int i = 0; i < fromFile.length; i++) { | ||
if (fromFile[i].toLowerCase().startsWith("w")) { | ||
resultArray[index] = fromFile[i].toLowerCase(); | ||
index++; | ||
} | ||
} | ||
|
||
Arrays.sort(resultArray); | ||
|
||
return resultArray; | ||
} | ||
} |