Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

source.text (File Should Look Like) destination.text (File Should Look Like) imp

ID: 3789487 • Letter: S

Question

source.text (File Should Look Like)

destination.text (File Should Look Like)

import java io import java util. public class Copy FileCapitalized public static void main (String args) throws Exception String censored Words C (''ABC XYZ'' //add code (1) private static String replaceCensoredWords (String line String [1 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). Don't worry too much aboutwriting code to handlingexceptions (just declare them in method headers) Samples for source.txtanddestination.bt can be downloaded from Connect

Explanation / Answer

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();
       System.out.println(word);
       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