Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution #1331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class FileWork {
public String[] readFromFile(String fileName) {
//write your code here
return null;

File file = new File(fileName);
BufferedReader bufferedReader;
try {
bufferedReader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found ",e);
}
StringBuilder stringBuilder = new StringBuilder();
int value;
try {
value = bufferedReader.read();
} catch (IOException e) {
throw new RuntimeException(e);
}

if (value == -1) {
return new String[0];
}
try {
while (value != -1) {
stringBuilder.append((char) value);
value = bufferedReader.read();
}

} catch (FileNotFoundException e) {
System.out.println("File not found " + e);
} catch (IOException e) {
System.out.println("Can't read file " + e);
}

String[] textArray = stringBuilder.toString().replaceAll("\n", " ").toLowerCase()
.split(" ");
int counterW = 0;

for (String word: textArray) {

if (word.charAt(0) == 'w') {
counterW++;
}
}
String[] wordArray = new String[counterW];
int indexWordArray = -1;

for (String word: textArray) {
if (word.charAt(0) == 'w') {
indexWordArray++;
wordArray[indexWordArray] = word.replaceAll("[^a-zA-Z]", "");
}
}
Arrays.sort(wordArray);
return wordArray;
}
}

Loading