Files
Tutoring-APCompSci/Sorting/RandomWordFileReader.java
2020-07-03 16:48:35 -05:00

30 lines
954 B
Java

package Sorting;
import java.util.ArrayList;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
public class RandomWordFileReader {
private ArrayList<String> words = new ArrayList<String>();
public RandomWordFileReader(String filename){
try(BufferedReader reader = Files.newBufferedReader(Path.of(filename));){
//This should use flatMap to make a string array from the line, and then turn the elements of that array into a stream, which goes to forEach
reader.lines().flatMap(line -> Arrays.stream(line.split("[\\s\\W_]+"))).forEach(word -> words.add(word.toLowerCase()));
}catch (IOException ex){
System.err.println(ex);
}
/*for (String e : words) {
e = e.replaceAll("\\W+","");
}*/
}
public ArrayList<String> getWords(){
return words;
}
}