-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.java
83 lines (72 loc) · 2.61 KB
/
search.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
//import com.google.common.base.Splitter;
class search{
public static void main(String[] args){
String[] input;
File file;
Scanner scan = new Scanner(System.in);
Scanner fileScanner;
List<String> lines;
boolean isEOF = false;
String query;
while (!isEOF) {
try{
System.out.println("Enter command: ");
input = scan.nextLine().split(" ");
//Splitter.on(" ").split(input);
if(input.length < 3){
System.out.println("Too few arguments!\n");
} else {
file = new File(input[input.length-1]);
fileScanner = new Scanner(file);
query = concatQuery(input);
System.out.println("Searching for: \"" + query + "\" in file " + input[input.length - 1] + "\n");
if(input[0].equals("search")){
lines = searchQuery(query, fileScanner);
if(lines.isEmpty()){
System.out.println("No matches found!");
} else {
printMatches(lines);
}
} else {
System.out.println("Unknown command: " + input[0]);
}
}
} catch(FileNotFoundException e){
System.out.println("File not found!\n\n");
} catch(NoSuchElementException e) {
System.out.println("Exiting the program!");
isEOF = true;
}
}
scan.close();
}
private static List<String> searchQuery(String query, Scanner s){
List<String> matches = new ArrayList<String>();
for(int i = 1; s.hasNextLine(); i++){
String l = s.nextLine();
if(l.contains(query)){
matches.add(i + ": " + l + "\n");
}
}
return matches;
}
private static String concatQuery(String[] input){
for(int i = 2; i < input.length - 1; i++){
input[1] += " " + input[i];
}
return input[1];
}
private static void printMatches(List<String> lines){
System.out.print("Matches found at line(s): \n");
for(int i = 0; i < lines.size() - 1; i++){
System.out.print(lines.get(i));
}
System.out.println("\n");
}
}