-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNeedle.java
38 lines (35 loc) · 1.12 KB
/
Needle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package by.andd3dfx.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* <pre>
* Implement the function count(), which returns the number of lines from the InputStream
* that contains text matching the provided string.
*
* For example, when the `String needle` is "there" and the `InputStream haystack` contains:
*
* Hello, there!
* How are you today?
* Yes, you over there.
*
* The count() function should return 2: {"Hello, there!" and "Yes, you over there."}.
* </pre>
*
* @see <a href="https://youtu.be/XDIgxvYAGfY">Video solution</a>
*/
public class Needle {
public static int count(String needle, InputStream haystack) throws IOException {
try (var bufferedReader = new BufferedReader(new InputStreamReader(haystack))) {
String line;
var count = 0;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(needle)) {
count++;
}
}
return count;
}
}
}