From 60e60e497f6edfecbce097eb739719019af32b75 Mon Sep 17 00:00:00 2001 From: Bohdan Markatov Date: Tue, 2 Jan 2024 22:46:09 +0200 Subject: [PATCH] implement FileWork method --- src/main/java/core/basesyntax/FileWork.java | 35 +++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..d18e1a4e 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -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; } }