Question
Challenge: Document Merger Description: Merge text documents provided by the user into a new file. Purpose: This application provides experience with user input and interaction
Challenge: Document Merger
Description: Merge text documents provided by the user into a new file.
Purpose: This application provides experience with user input and interaction in the Console, writing files to disk, reading files from disk, working with exceptions, and writing programs in C#/.NET.
Requirements:
Project Name: Document Merger Target Platform: Console Programming Language: C#
Documentation:
- Types and variables (Links to an external site.) (Microsoft)
- Console.ReadLine Method () (Links to an external site.) (Microsoft)
- Strings (C# Programming Guide) (Links to an external site.) (Microsoft)
- How to read from and write to a text file by using Visual C# (Links to an external site.) (Microsoft)
- StreamWriter Class (Links to an external site.) (Microsoft)
- Exceptions and Exception Handling (C# Programming Guide) (Links to an external site.) (Microsoft)
This program is to display the name of the app Document Merger followed by a blank line, prompt the user for the names of two documents (which should be in the current directory) and save a new document in the current directory, and display a message if it was successful or unsuccessful based on a specification provided. The filename of the document is the names of the provided files merged together. Verify that the filenames the user enters exist at the point they enter the name. If the file doesnt exist, give the user feedback that the file doesnt exist and prompt them for another filename.
When the program runs:
- Display Document Merger followed by a blank line.
- Prompt the user for the name of the first text file.
- Verify that the first file exists. If not, give the user feedback and let them re-enter the first filename.
- Prompt the user for the name of the second document.
- Verify that the second file exists. If not, give the user feedback and let them re-enter the second filename.
- Merge the two file names and append .txt to the end (this includes removing the existing .txt extensions from the names).
- Read and merge the text of the two files.
- Save the content to a file in the current directory.
- If an exception occurs, output the exception message and exit.
- If an exception does not occur, output [filename] was successfully saved. The document contains [count] characters. and exit. [filename] and [count] are placeholders for the filename of the document and the number of characters it contains.
- After the program does or does not fail to merge the files, ask the user if they would like to merge two more files. If they do, prompt them again for input. If not, exit the program.
Note that the requirements say the files should all be in the current directory. This means you can use just the file name as the path for the file. If you do that, the file will be saved/read in the current directory for the application. When I created the project and called it Document Merger, a directory called Document Merger was created that contains Document Merger.sln and another directory called Document Merger. The Document Merger directory inside of the project is the current directory when the application runs and is where you will find the files that are saved.
Close open streams in the finally block of a try-catch-finally. Caution: test a variable holding a reference to a stream to make sure it is not null before calling the Close method on it. Calling a method on a null reference will crash the application.
Optional Requirements:
The following requirements are optional, but you should try to implement them if you can.
Instead of always using the merged names of the files for the new file name, prompt the user for a filename and present them with the default of the merged names if they dont enter a name (e.g. they choose the default by just hitting enter). Also, if they dont supply an extension of .txt, append .txt on the end.
Example: Enter new filename (default: File1File2.txt):
Allow the user to merge more than two files. Prompt the user for the names of files to merge until they dont supply any more files. So, at the first prompt the user enters a filename, at the second prompt they enter a filename, and at a third prompt they enter a filename. Then, at the fourth prompt, they hit enter indicating they are done entering filenames. So, repeatedly ask for filenames and put them in a list of files to merge until the user signals no more files by supplying no filename at a prompt and just hitting enter. Remember, if a file doesnt exist, give the user feedback that the file doesnt exist and prompt them for another filename immediately after they enter the filename.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started