Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a JAVA program that will: ask the user to enter the name of an input data file read the contents of the file and

Write a JAVA program that will:

ask the user to enter the name of an input data file

read the contents of the file and display some statistics about the information found in the file

create an output file which has the same name as the input file, except that the word "copy" will be attached to the end of the filename

in the output file, create a copy of the input file except that it should be double-spaced

if either the input or the output files cannot be opened, display an error message stating that the file cannot be opened, and then stop the program - you do not need to specify which one could not be opened

Statistics

Your program must read the contents of the input file and print the following statistics to the screen:

The number of characters in the file.

The number of words in the file.

The number of lines in the file.

The number of paragraphs in the file.

File Copy

Your program must also create an output file. The name of the output file should be the same name as the input file, except that it should have the word copy attached to the end of the filename. As an example, if the input filename is data.txt, then the output filename should be datacopy.txt. Your program should copy all of the information from the input file into the output file, except that new file should be double spaced. That is, your program should insert a blank line after every line that it copies into the new file.

Example Screen Output

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:

Enter the name of the input file: data.txt Characters: 1495 Words: 283 Lines: 22 Paragraphs: 4 Output file datacopy.txt created.

Example File Output

If the above data.txt file was used for input, then the output file should contain the following information. Please note that to save space, only a portion of the output file is shown here.

Four score and seven years ago our fathers brought forth ... Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, ... dedicated, can long endure. We are met on a great battle-field of that ... portion of that field, as a final resting place for those who here ...

Technical notes and restrictions:

Number of characters: This means all characters, including spaces and tabs. Do not count the line breaks. The sample data.txt file contains 1495 characters.

Number of words: A word is defined as any sequence of non-whitespace characters. This also include punctuation marks. As an example, the string "my.dog" would be counted as one word, and so would the string "---". The sample data.txt file contains 283 words.

Number of lines: This is essentially the number of line breaks in the file. A line is counted even if it blank. Note that the last line in the file may or may not have a line break after it but should be counted either way. The sample data.txt file contains 22 lines.

Number of paragraphs: This is defined as any sequence of non-blank lines. The sample data.txt file contains 4 paragraphs.

Absolutely NO global variables (class variables) are allowed.

DO NOT try to read the entire contents of the input file into a huge array in memory! Files can hold a lot more data than memory can and therefore, it is possible that the data.txt file could have too much information in it to store into an array in your program. You are free to use arrays if you feel it will help you, just as long as you are not trying to read the entire file contents into the array all at once.

DO NOT read the input file more than once! You can accomplish this entire program by reading the input file only one time. It is inefficient to have your program read the input file multiple times. As such, 5 points will be deducted for each additional time you read the file after reading it the first time.

You are required to write and use at least ONE method in this program (in addition to the main method). You are free to write whatever method(s) you feel will be helpful to you. Any methods you create must have a very clear purpose and perform a very specific task. You cannot just simply write a simple printing method just for the purposes of meeting this requirement!

Your methods must be commented. To repeat again, this means a comment before the method which states the purpose of the method, the purpose of each of its parameters, and the purpose of its return value.

Remember that at this stage, COMMENTING IS A REQUIREMENT! Make sure you FULLY comment your program. You must include comments that explain what sections of code is doing. Notice the key word "sections"! This means that you need not comment every line in your program. Write comments that describe a few lines of code rather than "over-commenting" your program.

Build incrementally. Don't tackle the entire program at once. Write a little section of the program, then test it AND get it working BEFORE you go any further. Once you know that this section works, then you can add a little more to your program. Stop and test the new code. Once you get it working, then add a little bit more.

Make sure you FULLY test your program! Make sure to run your program multiple times with different data in your text file. Feel free to change the data in the data.txt file. You are not required to keep the data that you were given for your data.txt file. This was supplied only as a convenience for testing.

data.txt file =

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth. Abraham Lincoln November 19, 1863 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions