-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ4.java
More file actions
38 lines (29 loc) · 1.16 KB
/
Q4.java
File metadata and controls
38 lines (29 loc) · 1.16 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
import java.io.*;
import java.util.*;
public class Q4 {
public static void main(String[] args) throws Exception{
String censoredWord[] = {"ABC", "XYZ"};
//add code (1)
File source = new File("./src/source.txt");
Scanner scan = new Scanner(source);
PrintWriter writer = new PrintWriter("./src/destination.txt");
String upperLine = "";
while(scan.hasNext()){
//Sends it to the replaceCensoredWords method and changes it to uppercase, then compares it to censoredWord array.
upperLine= replaceCensoredWords(scan.nextLine().toUpperCase(), censoredWord);
writer.println(upperLine);
}
System.out.println("File copy complete!");
writer.close();
scan.close();
}
private static String replaceCensoredWords(String line, String[] censoredWords) {
//Checks the string to see if it contains a certain string. If it does, it replaces it with "..."
for(int i = 0 ; i < censoredWords.length ; i++){
if(line.contains(censoredWords[i])){
line = line.replaceAll(censoredWords[i] , "...");
}
}
return line;
}
}