From 998e9f487d65c6f618e9e6d85c9f94704bff8b80 Mon Sep 17 00:00:00 2001 From: eugenefischer Date: Fri, 3 Jul 2020 17:04:43 -0500 Subject: [PATCH] remove empty strings --- Sorting/RandomWordFileReader.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sorting/RandomWordFileReader.java b/Sorting/RandomWordFileReader.java index a1b4a3d..4bfa972 100644 --- a/Sorting/RandomWordFileReader.java +++ b/Sorting/RandomWordFileReader.java @@ -14,13 +14,16 @@ public class RandomWordFileReader { 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())); + reader.lines().flatMap(line -> Arrays.stream(line.split("[\\W\\s\\n_ ]+"))).forEach(word -> words.add(word.toLowerCase())); }catch (IOException ex){ System.err.println(ex); } - /*for (String e : words) { - e = e.replaceAll("\\W+",""); - }*/ + for (int i = 0; i < words.size(); i++) { + if (words.get(i).matches("")) { + words.remove(i); + i--; + } + } } public ArrayList getWords(){