Commit 8b76e81a authored by Filip Filchev's avatar Filip Filchev
Browse files

Add Reading until Blank Line

parent d4bfe7cb
Showing with 34 additions and 2 deletions
+34 -2
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
......@@ -13,7 +14,10 @@ public class Task1 {
where is the peck of pickled peppers peter piper picked
""";
public static void printWordRepetitions(Reader reader) {
/**
* Reads all the words from the Reader
*/
public static void readUntilEndOfReader(Reader reader) {
Scanner scanner = new Scanner(reader);
Map<String, Integer> map = new HashMap<>();
......@@ -29,8 +33,36 @@ public class Task1 {
}
}
/**
* Ends when we insert a blank line in the Console
*/
public static void readUntilBlankLine(InputStream stream) {
Scanner scanner = new Scanner(stream);
Map<String, Integer> map = new HashMap<>();
String line;
while (!(line = scanner.nextLine()).isBlank()) {
String[] split = line.split("\\s+");
for (String word : split) {
if (word.isBlank()) {
continue;
}
map.putIfAbsent(word, 0);
map.put(word, map.get(word) + 1);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
public static void main(String[] args) {
StringReader reader = new StringReader(INPUT);
printWordRepetitions(reader);
readUntilEndOfReader(reader);
readUntilBlankLine(System.in);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment