-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFilter.java
393 lines (287 loc) · 12.9 KB
/
TextFilter.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import java.util.Scanner;
/**
* TODO: DESCRIBE YOUR PROJECT HERE
* I am using this project for Text manipulation as per the
* requirements of the user.
*
* @author Amrish Nayak, [email protected]
* @version 02/09/18
*/
public class TextFilter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Print hello message
System.out.println("Welcome to TextFilter!");
// Value to keep track of if the user wants to keep filtering text
boolean keepFiltering;
do {
// Print options for the user to select
System.out.println("Please select one of the following filtering options: \n");
System.out.println("1. Filter Word\n" +
"2. Find-And-Replace\n" +
"3. Censor Information");
// Save their choice
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
// PART 1 - Censoring Words
String passage = ""; // The text to be filtered
System.out.println("Please enter the passage you would like filtered: ");
// TODO: PART 1 - Ask the user for a passage to censor
passage=scanner.nextLine();
while (passage.isEmpty()){
passage=scanner.nextLine();
}
String word; // The word to be censored from the text phrase
System.out.println("Please enter the word you would like to censor: ");
word=scanner.nextLine();
while (word.isEmpty()){
word=scanner.nextLine();
}
// TODO: PART 1 - Ask the user for a word to censor
System.out.println("Uncensored: ");
System.out.println(passage);
int m;
m=word.length();
int pasp=passage.length();
StringBuilder sb= new StringBuilder(passage);
String t=passage;
String p="";
for(int i=0;i<m;i++){
p=p+"X";
}
if (word.contains(" ")){
passage=passage.replaceAll(word,p);
}
else {
for (int i = 0; i < passage.length(); i++) {
if (passage.charAt(i) == ' ' || passage.charAt(i) == '!' || passage.charAt(i) == '?' || passage.charAt(i) == ',' || passage.charAt(i) == '.') {
if ((i-m)>=0) {
t = passage.substring((i - m), i);
if (t.equals(word)) {
if((i-m)>0) {
char ch = passage.charAt((i - m) - 1);
if (!Character.isLetter(ch)) {
sb = sb.replace((i - m), i, p);
passage = sb.toString();
}
}else {
sb = sb.replace((i - m), i, p);
passage = sb.toString();
}
}
}
}
if(passage.endsWith(word)){
sb=sb.replace((pasp-m),pasp,p);
passage=sb.toString();
}
}
}
// TODO: PART 1 - Implement your parsing here
System.out.println("Censored: ");
System.out.println(passage);
} else if (choice == 2) {
// PART 2 - Replacing Words
String passage = ""; // The text to be filtered
System.out.println("Please enter the passage you would like filtered: ");
// TODO: PART 2 - Ask the user for a passage to filter
passage=scanner.nextLine();
while (passage.isEmpty()){
passage=scanner.nextLine();
}
String replace; // The word to be filtered from the text phrase
System.out.println("Please enter the word you would like to replace: ");
//TODO: PART 2 - Ask the user for a word to replace
replace=scanner.nextLine();
while (replace.isEmpty()){
replace=scanner.nextLine();
}
String insert; // The word to be inserted in the text phrase
System.out.println("Please enter word you would like to insert: ");
//TODO: PART 2 - Ask the user for a word to insert
insert=scanner.nextLine();
while (insert.isEmpty()){
insert=scanner.nextLine();
}
System.out.println("Uncensored: ");
System.out.println(passage);
int m;
m=replace.length();
int pasp=passage.length();
StringBuilder sb= new StringBuilder(passage);
String t=passage;
// String p="";
// for(int i=0;i<m;i++){
// p=p+"X";
// }
if (replace.contains(" ")){
passage=passage.replaceAll(replace,insert);
}
else {
for (int i = 0; i < passage.length(); i++) {
if (passage.charAt(i) == ' ' || passage.charAt(i) == '!' || passage.charAt(i) == '?' || passage.charAt(i) == ',' || passage.charAt(i) == '.') {
if((i-m)>=0) {
t = passage.substring((i - m), i);
if (t.equals(replace)) {
sb = sb.replace((i - m), i, insert);
passage = sb.toString();
}
}
}
if(passage.endsWith(replace)){
sb=sb.replace((pasp-m),pasp,insert);
passage=sb.toString();
}
}
}
// TODO: PART 2 - Implement your parsing here
System.out.println("Censored: ");
System.out.println(passage);
} else if (choice == 3) {
// PART 3 - Censoring Personal Information
/*
* DO NOT ALTER THIS CODE! This formatting is imperative to the completion of this task.
*
* Keep asking for input until the user enters an empty line
* If they enter an empty line and the phrase is empty, keep waiting for input
*/
String passage = ""; // String for holding text to be filtered
System.out.println("Please enter the phrase you would like to censor information from: ");
while (true) {
// Obtain a line from the user
String temp = scanner.nextLine();
if (!passage.isEmpty() && temp.isEmpty()) {
//System.out.println("BREAK");
break;
} else if (passage.isEmpty() && temp.isEmpty()) {
//System.out.println("continue");
continue;
}
// Add the contents of temp into the phrase
passage += temp;
// Append a newline character to each line for parsing
// This will separate each line the user enters
// To understand how input is formatted in Part 3, please refer to the handout.
passage += '\n';
}
// Print the uncensored passage
System.out.println("Uncensored: ");
System.out.println(passage);
// TODO: PART 3 - Implement your parsing here
StringBuilder ts=new StringBuilder(passage);
int m=0;
int n=0;
int ln=0;
String tn=passage;
while(true){
if(tn.contains("Name:")) {
n = passage.indexOf("Name:", n);
n=n+7;
m= passage.indexOf('\n',n);
ln=tn.indexOf("Name: ",ln);
ln=ln+5;
tn = tn.substring(ln);
//System.out.println("N"+n);
if (n!=6) {
for (int i = n; i < m - 1; i++) {
char ch = passage.charAt(i);
if (ch != ' ') {
ts.setCharAt(i, '*');
passage = ts.toString();
}
}
}
}
else {
break;
}
}//while name
boolean tf=true;
int m1=0;
int n2=0;
int n3=0;
int n4=0;
String mt=passage;
while(tf) {
if (mt.contains("Email:")) {
m1= passage.indexOf("Email:",m1);
m1=m1+8;
n2 = passage.indexOf('\n',m1);
n4=passage.lastIndexOf('.',n2);
n3 =mt.indexOf("Email:",n3);
n3=n3+1;
mt= mt.substring(n3);
// System.out.println(mt);
}
else {
tf=false;
break;
}
// System.out.println("E"+m1);
if(m1!=7){
for (int j = m1; j < n4; j++) {
if (passage.charAt(j) == '@') {
j++;
} else {
ts.setCharAt(j, '*');
passage = ts.toString();
}
}
}
//System.out.println(passage);
}//while email
String abc=passage;
int a=0;
int b=0;
int c=0;
while(true) {
if (abc.contains("Phone:")) {
a = passage.indexOf("Phone: ",a);
a=a+7;
b=passage.indexOf('\n',a);
c=abc.indexOf("Phone: ", c);
c=c+1;
abc=abc.substring(c);
// System.out.println("P"+a);
if(a!=6) {
for (int k = a; k < b - 4; k++) {
if (passage.charAt(k) != '-') {
ts.setCharAt(k, '*');
passage = ts.toString();
}
}
}
}else {
break;
}
}//while phone
// Print the censored passage
System.out.println("Censored: ");
System.out.println(passage);
} else {
// They entered a number that was not 1, 2 or 3
System.out.println("The option you chose was invalid!");
}
System.out.println("Would you like to keep filtering? Yes/No");
// TODO: PART 4 - Update the value of keepGoing accordingly
String ans;
boolean keep;
ans= scanner.nextLine();
ans=ans.toLowerCase();
while(ans.isEmpty()){
ans= scanner.nextLine();
ans=ans.toLowerCase();
}
if(ans.equals("yes")){
keep=true;
}
else {
keep=false;
}
// TODO: PART 4 - Replace the line below with your code
keepFiltering = keep;
} while (keepFiltering);
System.out.println("Thank you for using TextFilter!");
}
}