1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
finlep [7]
3 years ago
8

Write a program that reads a file containing text. Read each line and send it to the output file, preceded by line numbers. If t

he 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! The line numbers are enclosed in /* */ delimiters so that the program can be used for numbering Java source files. Prompt the user for the input and output file names.

Computers and Technology
1 answer:
Shtirlitz [24]3 years ago
7 0

Answer:

I am writing a JAVA program:

import java.io.FileReader;  // class used for reading data from a file

import java.io.IOException;  //class used for handling exceptions

import java.io.PrintWriter;  // class used to printing or writing formatted data. It is used when the data to be written contains text and numbers

import java.util.Scanner;   // class used to take input from the user

public class Main  {  //start of the class

public static void main(String[] args)  {//start of main() function body

Scanner input = new Scanner(System.in);  //creates Scanner class object

System.out.print("Enter the input file name: ");  // prompts user to enter the input file name

String input_file = input.next();  //to  read and return the token i.e. input file name and store this into input_file String type variable

System.out.print("Enter the output file name: "); // prompts user to enter the output file name

String output_file = input.next();   /to  read and return the token i.e. output file name and store this into output_file String type variable

try   {/* try  defines a chunk of code to be tested for errors during the execute of this chunk. its a code block for reading from input file and writing to output file. This handles the exception if program cannot create or write to the file indicated. */

   FileReader inFile = new FileReader(input_file);  //creates object inFile of FileReader class to read the input_file contents

   Scanner scan = new Scanner(inFile);  //read and scans the input file

   PrintWriter outFile = new PrintWriter(output_file);  //creates object outFile of PrintWriter class to write to the output file. It is used to write output data in a readable text form.

   int count = 1;    //counts the line number      

   while (scan.hasNextLine())  {  //loop keeps checking for the next line in input file

    String lines = scan.nextLine();  // reads the entire line of the input file and stores that line contents in String lines

    outFile.println("/* " + count + " */ " + lines);  //each line represented with line number enclosed in /* */

    count++;  }   //increments the line number at each iteration    

    outFile.close();        }  //closes the output file after writing the contents from input file with each line of the input file enclosed in  /* */

catch (IOException e)  {  // catching the IOException and prints the following message if program cannot create input_file or write to the output_file

   System.out.println("Error processing file: " + e);    }     }   }

Explanation:

The program is well explained in the comments mentioned with each statement of the above program. The user is prompted to enter input and output file names. Then the program uses a while loop that scans through lines of the input file and count variable counts each line of input file. Then at each iteration the count variable counts a line of input file and line number of each line of the input file is enclosed in /* */ delimiters. The contents of input file, in which the line numbers of text lines of the input file are enclosed in /* */ delimiters, is written to the output file.

The screenshot of the program is attached along with the output file produces as a result of this program execution.

You might be interested in
In python please!! Write the definition of a function named countPos that needs integer values from standard input until there a
jek_recluse [69]

Answer:

Explanation:

The following code is written in Python it doesn't use any loops, instead it uses a recursive function in order to continue asking the user for the inputs and count the number of positive values. If anything other than a number is passed it automatically ends the program.

def countPos(number=input("Enter number: "), counter=0):

   try:

       number = int(number)

       if number > 0:

           counter += 1

           newNumber = input("Enter number: ")

           return countPos(newNumber, counter)

       else:

           newNumber = input("Enter number: ")

           return countPos(newNumber, counter)

   except:

       print(counter)

       print("Program Finished")

countPos()

8 0
3 years ago
I am trying to write a c++ program like the game typeracer. When the player starts the program he will be asked to write a sente
murzikaleks [220]
Http://www.cprogramming.com/tutorial/string.html

--------------------------------------------------------------------
6 0
3 years ago
Inputting a range of numbers comprising a batch and then inputting each serially numbered document is characteristic of the cont
Fynjy0 [20]

Answer:

batch sequence check.

Explanation:

A batch sequence check can be defined as a strategic and systematic control plan which typically involves the process of inputting a range of numbers comprising a batch and then inputting each serially numbered document.

The steps for checking an event data within a batch using a batch sequence check include the following;

I. You'll enter a range of serial numbers of the document in a batch.

II. You'll enter each serially pre-numbered document one after the other.

III. The input documents are sorted by a computer into a serial (numerical) order, match the sequence number range against the input documents, and then reports any part of the document that is missing, a duplicate and out of range.

6 0
3 years ago
Which of these completes the sentence? People interpret information ___________. Choose one.
evablogger [386]

Explanation:

mannejaondpsbma0jdjdhdj

5 0
3 years ago
Read 2 more answers
Carlos works with a tutor all school year to maintain good grades in his language arts class. However, his tutoring sessions are
Ksivusya [100]

Answer:

I would pick A.

Explanation:

Letting yourself know that making mistakes is a good way to let yourself know you won't completely fail.

8 0
2 years ago
Read 2 more answers
Other questions:
  • Yan wants to attract customers specifically searching on Google for Time-B-Gone, his company's unique office-support product. Hi
    12·1 answer
  • An example of software most commonly associated with productivity software is ____.
    12·1 answer
  • In CadStd, what does the Zoom All button do? A. Makes a drawing bigger so you can see small details B. Returns a drawing to a si
    5·1 answer
  • PLEASE HELP ANSWER THIS only if you know BOTH ANSWERS!
    9·2 answers
  • Text that is heavier or darker than other text in the document is considered _____.
    9·2 answers
  • What are the elements of a good study environment? Check all that apply.
    9·2 answers
  • You complete your database, and the company begins using it. Shortly afterwards, two GearUp buyers, Cora and Owen, work together
    9·1 answer
  • Who watches Riverdale ? if you do can we be friends(pLEASE DON'T DELETE) and also who is your fav character from Riverdale
    8·2 answers
  • Please Help ASAP. Marking Brainliest For Correct Answer.
    5·1 answer
  • What are some examples of productions categorized as non-broadcast productions?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!