Write a program that reads lines from a file and prints them out, removing all occurrences of a specified string from the lines.

Write a program that reads lines from a file and prints them out, removing all occurrences of a specified string from the lines.

LANGUAGE: Java

CHALLENGE:

Write a program that reads lines from a file and prints them out, removing all occurrences of a specified string from the lines.
For example, if the class that housed your program was called Exercise12_11, running this prompt on the command line:
java Exercise12_11 John filename
Would print out the contents of the file filename without any instances of the string John.
Your program should get the arguments from the command line.
SAMPLE RUN #1: java RmvText going input
Hide Invisibles
Highlight: Show Highlighted Only
I’m·to·the·market.↵
How’s·it?↵
Things·are·great.↵
You’d·better·get!↵

SOLUTION:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class RmvText {
   public static void main(String[] args) throws FileNotFoundException {
       String word = args[0];
       String filename = args[1];
       Scanner scan = new Scanner(new File(filename ));
       while(scan.hasNextLine()){
           String line = scan.nextLine();
           line = line.replaceAll(word," ");
           System.out.println(line);
       }
   }
}

Add comment