-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryTheFile.java
More file actions
35 lines (29 loc) · 1.3 KB
/
QueryTheFile.java
File metadata and controls
35 lines (29 loc) · 1.3 KB
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
package problems;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class QueryTheFile {
public static void main(String[] args) throws IOException {
// Assign String Arraylist date type variable to solution function
ArrayList<String> result = solution();
// Print result Arraylist
for (String i: result) {
System.out.println(i);
}
}
// This function reads input from a text file and filters
// out strings that contain the number 2.
// This is done with the filter stream method.
public static ArrayList<String> solution() throws IOException {
// The text file used in this function is stored in the folder /src/problems/textfiles/.
// The string value used fro the file name is relative. Make sure to include the file
// path the text file is stored in.
String fileName = "C:\\Users\\hbreck\\Documents\\Basic-Java-Problems\\src\\problems\\textfiles\\filter_problem.text";
Stream<String> stream = Files.lines(Paths.get(fileName));
return stream.filter(string -> !string.contains("2"))
.collect(Collectors.toCollection(ArrayList::new));
}
}