Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Paysafe Interns Valentin-Martin
Java Collections
filip-filchev-java-collections-2023
Commits
8b76e81a
Commit
8b76e81a
authored
1 year ago
by
Filip Filchev
Browse files
Options
Download
Email Patches
Plain Diff
Add Reading until Blank Line
parent
d4bfe7cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Task1.java
+34
-2
src/Task1.java
with
34 additions
and
2 deletions
+34
-2
src/Task1.java
+
34
-
2
View file @
8b76e81a
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
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help