-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSearchMilestone2.java
More file actions
46 lines (45 loc) · 1.28 KB
/
StringSearchMilestone2.java
File metadata and controls
46 lines (45 loc) · 1.28 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
36
37
38
39
40
41
42
43
44
45
46
import java.nio.file.*;
import javax.swing.TransferHandler;
import java.io.IOException;
class FileHelper {
static String[] getLines(String path) {
try {
return Files.readAllLines(Paths.get(path)).toArray(String[]::new);
}
catch(IOException e) {
System.err.println("Error reading file " + path + ": " + e);
return new String[]{"Error reading file " + path + ": " + e};
}
}
}
class ContainsQuery{
String s;
ContainsQuery(String s){
this.s = s;
}
boolean matches(String check){
if(check.contains(this.s)){
return true;
}
else{
return false;
}
}
}
class StringSearch{
public static void main(String[] args){
String[] lines = FileHelper.getLines(args[0]);
if(args.length == 1){ //prints all lines of given file
for(int i =0; i<lines.length; i++){
System.out.println(lines[i]);
}
}
else if(args.length == 2){
ContainsQuery current = new ContainsQuery(args[1]);
for(int i = 0; i < lines.length; i++){ //loops through every line
if(current.matches(lines[i]))
System.out.println(lines[i]);
}
}
}
}