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

Java Chapter 12 **12.11 ( Remove text ) Write a program that removes all the occ

ID: 3864371 • Letter: J

Question

Java Chapter 12 **12.11 (Remove text) Write a program that removes all the occurrences of a specified string from a text file. For example, invoking java Exercise12_11 John filename removes the string John from the specified file. Your program should get the arguments from the command line

This is my code

RemoveText.java:

import java.io.*;
import java.util.*;

public class RemoveText {
   // main method.
   public static void main(String[] args) {
      
       // Checking for invalid command line entry.
       if (args.length != 2) {
           System.out.println("Usage: java RemoveText <string to remove> <fileName>");
           System.exit(0);
       }

       File sourceFile = new File(args[1]);
      
       // Checking the existence of the file.
       if (!sourceFile.exists()) {
           System.out
                   .println("Source file does not exist. Program will exit. ");
           System.exit(0);
       }
      
       File tempFile = new File("temp_" + args[1]);

       // Creating the Scanner object.
       Scanner input = null;
       try {
           input = new Scanner(sourceFile);
       } catch (FileNotFoundException e) {
           // this will never be executed as we have already checked that file exists
       }

       // Creating the PrintWriter object.
       PrintWriter output = null;
       try {
           output = new PrintWriter(tempFile);
       } catch (FileNotFoundException e) {
           // if file doesn't exist it creates the file
           e.printStackTrace();
       }
      
       while (input.hasNext()) {
           String s1 = input.nextLine();
           // Removing the text.
           String s2 = s1.replaceAll(args[0], "");
           output.println(s2);
       }
      
       input.close(); // Closing the stream.
       output.close(); // Closing the stream.
       sourceFile.delete(); // Deleting the original file.
      
       tempFile.renameTo(sourceFile);
      
       // Renaming the temp file.
       tempFile.delete(); // Deleting the temp file.
   }
}

input file: input
Hi My Name is Jaydan. Jaydan is writing a code for question 12.11 (Remove text)
Jaydan feels good about it.
Now to run the executable, enter the commands in format: java RemoveText <string to remove> <fileName> but is just does not remove Jaydan

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below.

RemoveText.java

import java.io.*;
import java.util.*;
public class RemoveText {
// main method.
public static void main(String[] args) {
  
// Checking for invalid command line entry.
if (args.length != 2) {
System.out.println("Usage: java RemoveText <string to remove> <fileName>");
System.exit(0);
}
File sourceFile = new File(args[1]);
  
// Checking the existence of the file.
if (!sourceFile.exists()) {
System.out
.println("Source file does not exist. Program will exit. ");
System.exit(0);
}
  
File tempFile = new File( "D:\tempfile.txt" );
// Creating the Scanner object.
Scanner input = null;
try {
input = new Scanner(sourceFile);
} catch (FileNotFoundException e) {
// this will never be executed as we have already checked that file exists
}
// Creating the PrintWriter object.
PrintWriter output = null;
try {
output = new PrintWriter(tempFile);
} catch (FileNotFoundException e) {
// if file doesn't exist it creates the file
e.printStackTrace();
}
  
while (input.hasNextLine()) {
String s1 = input.nextLine();
// Removing the text.
String s2 = s1.replaceAll(args[0], "");
output.println(s2);
}
output.flush();
input.close(); // Closing the stream.
output.close(); // Closing the stream.
sourceFile.delete(); // Deleting the original file.
  
tempFile.renameTo(sourceFile);
  
// Renaming the temp file.
tempFile.delete(); // Deleting the temp file.
}
}

Output:

inputfile.txt

Hi My Name is . is writing a code for question 12.11 (Remove text)
feels good about it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote