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

FOR JAVA Write a program that takes two command line arguments: an input file an

ID: 3573467 • Letter: F

Question

FOR JAVA

Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message.

Notes:

This program can be written in a single main method

Remember that Scanner objects can be opened on the keyboard, a file, or a String.

You may assume that the file ends with a newline sequence (i.e., the file ends with ).

A sample text file is attached.

Sample Text:

Explanation / Answer

package com.files.characters;

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

public class ScannerCharReplacer {

   public static void main(String args[]) {

       if (args.length < 2) {
           throw new RuntimeException(
                   "Please provide the names of the input and output files as command line arguments");
       }
       // Creating File instance to reference text
       File inputFile = new File(args[0]);

       // Creating a Scanner instance to read File
       Scanner scanner;
       try {
           scanner = new Scanner(inputFile);
       } catch (FileNotFoundException e) {
           throw new RuntimeException(
                   "Did not find the input file. Please provide a valid path name for the input file");
       }

       PrintWriter out;
       try {
           out = new PrintWriter(args[1]);
       } catch (FileNotFoundException e) {
           throw new RuntimeException(
                   "Cannot create and output file. Please provide the name of existing, writable and output file or a path at where this output file cas be created");
       }
       // Reading each line of file using Scanner class
       int lastIndex;
       String line = null;
       String[] words;
       while (scanner.hasNextLine()) {
           line = scanner.nextLine();
           words = line.split(" ");
           for (String word : words) {
               lastIndex = word.length() - 1;
               word = word.substring(0, lastIndex) + "*";
               out.print(word + " ");
           }
           out.println("");
       }
       scanner.close();
       out.close();
   }

}