Skip to content

Commit

Permalink
Complete task
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriyM250 committed Sep 26, 2024
1 parent 160910a commit f2109fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import core.basesyntax.FileWork;
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
FileWork work = new FileWork();
System.out.println(Arrays.toString(work.readFromFile("test2")));
String a = "";
}
}
43 changes: 41 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
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;
int counter = 0;
File file = new File(fileName);
String [] out;

try {
String array = Files.readAllLines(file.toPath()).toString();
String [] strings = array.split("\\W+");

for (int i = 0; i < strings.length; i++) {
if (strings[i].isEmpty()) {
continue;
}
if (strings[i].charAt(0) == 'w' || strings[i].charAt(0) == 'W') {
counter++;
}
}
if (counter == 0) {
return new String[0];
} else {
out = new String[counter];
for (int i = 0, j = 0; i < strings.length; i++) {
if (strings[i].isEmpty()) {
continue;
}
if (strings[i].charAt(0) == 'w' || strings[i].charAt(0) == 'W') {
out[j] = strings[i].toLowerCase();
j++;
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// Sort by alphabet
Arrays.sort(out);
return out;
}
}

0 comments on commit f2109fd

Please sign in to comment.