import javaio import javautil public class CopyFileCapitaliz

import java.io.*;

import java.util.*;

public class CopyFileCapitalized {

public static void main(String[] args) throws Exception {

String censoredWords[] = {\"ABC\", \"XYZ\"};

//add code (1)

}

private static String replaceCensoredWords(String line, String[] censoredWords){

//add code (2)

}

}

Write code at (2) to check line word by word and replace those which are listed in the censoredWords array with \"...\" (three dots). The method should eventually return the same line of text after replacing the censored words. One way to check the words in line is to use the following statement and then read the words from the input stream using next().

Scanner input = new Scanner(line);

Write code at (1) to read the contents of a text file (e.g., source.txt) line-by-line, replace censored words using the replaceCensoredWords method, convert the text to uppercase, and write it to a destination file (e.g., destination.txt).

source.txt

abc def ghi abcdef

123 jkl mno

pqr 456 stu

vw xyz 789 vwxyz

destination.txt

... DEF GHI ABCDEF

123 JKL MNO

PQR 456 STU

VW ... 789 VWXYZ

import java.io.*;

import java.util.*;

public class CopyFileCapitalized {

public static void main(String[] args) throws Exception {

String censoredWords[] = {\"ABC\", \"XYZ\"};

//add code (1)

}

private static String replaceCensoredWords(String line, String[] censoredWords){

//add code (2)

}

}

Solution

CopyFileCapitalized.java

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class CopyFileCapitalized {
public static void main(String[] args) throws Exception {
String censoredWords[] = {\"ABC\", \"XYZ\"};
Scanner scan = new Scanner(new File(\"D:\\\\source.txt\"));
PrintWriter pw = new PrintWriter(new File(\"D:\\\\destination.txt\"));
while(scan.hasNextLine()){
   String line = scan.nextLine();
   String returnLine = replaceCensoredWords(line, censoredWords);
   pw.println(returnLine);
}
pw.flush();
pw.close();
System.out.println(\"File has been generated\");
}
private static String replaceCensoredWords(String line, String[] censoredWords){
//add code (2)
   String returnString = \"\";
   boolean isCensored = false;
   Scanner scan = new Scanner(line);
   while(scan.hasNext()){
       isCensored = false;
       String word = scan.next().toUpperCase();
for(String s: censoredWords){
           if(s.equals(word)){
               returnString = returnString + \"...\"+\" \";
               isCensored = true;
               break;
           }
       }
       if(!isCensored){
           returnString = returnString + word+\" \";
              
       }
      
   }
   return returnString;
}
}

Output:

File has been generated

destination.txt

... DEF GHI ABCDEF

123 JKL MNO

PQR 456 STU

VW ... 789 VWXYZ

import java.io.*; import java.util.*; public class CopyFileCapitalized { public static void main(String[] args) throws Exception { String censoredWords[] = {\
import java.io.*; import java.util.*; public class CopyFileCapitalized { public static void main(String[] args) throws Exception { String censoredWords[] = {\
import java.io.*; import java.util.*; public class CopyFileCapitalized { public static void main(String[] args) throws Exception { String censoredWords[] = {\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site