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

JAVA , write simple program , work up to chapter 6 only The purpose of this file

ID: 3839634 • Letter: J

Question

JAVA , write simple program , work up to chapter 6 only

The purpose of this file is to create a program that will read a Mad Lib file, allow the user to check for points to place the new words. Then output the result to a new text file. Words in the original file that need to be replaced begin and end with (). For example, the first line of Raven Mad Lib.txt looks like:

Anytime your program finds a word that start with a parenthesis '(' it should then ask the user to replace that word with whatever is inside the "()"

A sample Execution of the code would look like the following:

Then in that file should be the replacement file.

Notes

For the file names, you will want to do nextLine() in case they use spaces.

All replacement words start and end with parenthesis. () so you know what to look for.

So you will nee to both read and write from a file at the same time.

Most of the time you will just read a word and then turn around and write it to a file.

You don't need to worry about newline characters, your output can just be a single long line.

You should not however have a single word per line.

If you want to get fancy, you could put a newline character every 10th word.

Explanation / Answer

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Replacement {

    private static String INPUTFILEPATH = "";
    private static String OUTPUTFILEPATH = "";

    public static void main(String[] args) throws FileNotFoundException, IOException {
        String currentLine = "";
        /* reading inout and output file paths. please provide full file path
         example C:/Users/Junaid/Desktop/input.txt
                 C:/Users/Junaid/Desktop/output.txt
        */
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the input file Name");
        INPUTFILEPATH = scanner.next();
        System.out.println("Enter the Output file Name");
        OUTPUTFILEPATH = scanner.next();
        StringBuffer sb = new StringBuffer();
        StringBuffer sb1 = null;
        BufferedReader br = new BufferedReader(new FileReader(INPUTFILEPATH));
        /* read each line from inout file and ask user to replace the (text) and appen to stringbuffer br*/
        while ((currentLine = br.readLine()) != null) {
            String[] split = currentLine.split(" ");
            for (int i = 0; i < split.length; i++) {
                sb1 = new StringBuffer(split[i]);
                if (split[i].indexOf("(") > -1 && split[i].indexOf(")") > -1) {
                    System.out.println("While reading your file I found " + split[i].substring(split[i].indexOf("("), split[i].lastIndexOf(")") + 1) + " please type the replacement:");
                    sb1.replace(split[i].indexOf("("), split[i].lastIndexOf(")") + 1, scanner.next());
                    sb.append(sb1.toString() + " ");
                } else {
                    sb.append(split[i] + " ");
                }
            }
            sb.append(System.getProperty("line.separator"));
        }
        /* writing data to output file */
        BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUTFILEPATH));
        bw.write(sb.toString(), 0, sb.toString().length());
        System.out.println("your file is done, The output should be in " + OUTPUTFILEPATH);
        /* closing all resources */
        scanner.close();
        br.close();
        bw.close();
    }
}