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

Java Help: Write a program that reads a file containing text. Read each line and

ID: 3697980 • Letter: J

Question

Java Help:

Write a program that reads a file containing text. Read each line and send it to the output file, proeceded by line numbers. If the input file is:

Mary had a little lamb

Whose Fleece was white as snow

and everywhere that Mary went,

The lamb was sure to go!

then the program produces the output file

/* 1 */ Mary had a little lamb

/* 2 */ Whose Fleece was white as snow

/* 3 */ and everywhere that Mary went,

/* 4 */ The lamb was sure to go!

yhe line number are enclosed in /* */ delimiters so that the program can be used for numberin Java source files.

prompt the user for the input and output file names.

Explanation / Answer

input.txt

Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!

output.txt

/* 1 */ Mary had a little lamb

/* 2 */ Whose Fleece was white as snow

/* 3 */ and everywhere that Mary went,

/* 4 */ The lamb was sure to go!

Test.java


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

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter name of file for reading: ");
        String fileNameReading = input.next();
        System.out.print("Enter name of file for writing: ");
        String fileNameWriting = input.next();
        input.close();

        File fileReading = new File(fileNameReading);
        Scanner in = null;
        File fileWriting = new File(fileNameWriting);
        PrintWriter out = null;

        try {
            in = new Scanner(fileReading);
            out = new PrintWriter(fileWriting);
        } catch (FileNotFoundException e1) {
            System.out.println("Files are not founded!");
        }

        int lineNumber = 1;
        while (in.hasNextLine()) {
            String line = in.nextLine();
            out.write(String.format("/* %d */ %s ", lineNumber, line));
            lineNumber++;
        }

        out.close();
        in.close();

        System.out.println("File was read and written!");
    }
}

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