-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSearch.java
More file actions
164 lines (161 loc) · 4.64 KB
/
StringSearch.java
File metadata and controls
164 lines (161 loc) · 4.64 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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};
}
}
}
interface Query{
boolean matches(String s);
}
class ContainsQuery implements Query{
String s;
ContainsQuery(String s){
this.s = s.substring(s.indexOf("=")+1, s.length());
}
public boolean matches(String check){
if(check.contains(this.s)){
return true;
}
else{
return false;
}
}
}
class LengthQuery implements Query{ //based on the number of characters : NOT WORDS
int len;
LengthQuery(String q){
this.len = Integer.parseInt(q.substring(q.indexOf("=")+1, q.length()));
}
public boolean matches(String check){
if(check.length() == this.len)
return true;
else{
return false;
}
}
}
class GreaterQuery implements Query{
int num;
GreaterQuery(String s){
this.num = Integer.parseInt(s.substring(s.indexOf("=")+1), s.length());
}
public boolean matches(String check){
if(check.length() > num)
return true;
else{
return false;
}
}
}
class LessQuery implements Query{
int num;
LessQuery(String s){
this.num = Integer.parseInt(s.substring(s.indexOf("=")+1, s.length()));
}
public boolean matches(String check){
if(check.length() < num)
return true;
else{
return false;
}
}
}
class StartsQuery implements Query{
String word;
StartsQuery(String s){
this.word = s.substring(s.indexOf("=")+1, s.length());
}
public boolean matches(String check){
String begginingWord = check.substring(0, check.indexOf(" "));
if(begginingWord.equals(this.word))
return true;
else{
return false;
}
}
}
class EndsQuery implements Query{
String end;
EndsQuery(String s){
this.end = " " + s.substring(s.indexOf("=")+1, s.length());
}
public boolean matches(String check){
boolean answer = false;
if(check.contains(this.end)){
//check that the conditional is actually at the end of the string
String endOfCheck = check.substring(check.indexOf(this.end), check.length());
// System.out.println(endOfCheck);
if(endOfCheck.equals(end)){
answer = true;
}
}
return answer;
}
}
class NotQuery implements Query{
Query not;
NotQuery(Query q){
this.not = q;
}
public boolean matches(String check){
return (!this.not.matches(check));
}
}
interface Transform{
String transform(String s);
}
class StringSearch{
//takes query string in the form "<keyword>=<type>"
static Query readQuery(String q){
String keyword = q.substring(0, q.indexOf("="));
Query results;
if(q.contains("length")){
results = new LengthQuery(q);
}
else if(q.contains("greater")){
results = new GreaterQuery(q);
}
else if(q.contains("less")){
results = new LessQuery(q);
}
else if(q.contains("contains")){
results = new ContainsQuery(q);
}
else if(q.contains("starts")){
results = new StartsQuery(q);
}
else if(q.contains("ends")){
results = new EndsQuery(q);
}
else{ //not query
String query = q.substring(q.indexOf("("), q.length()-1); //appends to be just the new query statement
System.out.println("query passed into query reader = " + query);
results = readQuery(query);
}
return results;
}
public static void main(String[] args){ //one arguement in terminal
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){ //two arguments in terminal
Query current = readQuery(args[1]);
for(int i = 0; i < lines.length; i++){ //loops through every line
if(current.matches(lines[i])){
System.out.println(lines[i]);
}
}
}
}
}