Skip to content

Commit

Permalink
implement FileWork method
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohadan-Markatov committed Jan 2, 2024
1 parent 160910a commit 60e60e4
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
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;
}
}

0 comments on commit 60e60e4

Please sign in to comment.