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

please write in C# I will thumbs up on review !!! This program is to display the

ID: 3911933 • Letter: P

Question

please write in C#

I will thumbs up on review !!!

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 doesn't exist, give the user feedback that the file doesn't exist and prompt them for another filename.

When the program runs:

1. Display "Document Merger" followed by a blank line.

2. Prompt the user for the name of the first text file.

3. Verify that the first file exists. If not, give the user feedback and let them re-enter the first filename.

4. Prompt the user for the name of the second document.

5. Verify that the second file exists. If not, give the user feedback and let them re-enter the second filename.

6. Merge the two file names and append .txt to the end (this includes removing the existing .txt extensions from the names).

7. Read and merge the text of the two files.

8. Save the content to a file in the current directory.

9. If an exception occurs, output the exception message and exit.

10. 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.

11. 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.

Explanation / Answer

using System;
using System.IO;
using System.Text;

namespace Document_Merger
{
class Program
{

static void runOnce()
{
Console.WriteLine("Document Merger"); // print "Document Merger"
Console.WriteLine(); // print Blank Line
String first=""; // first filename variable
String second=""; // second filename variable
bool firstPrompt = true; // indicated whether prompted for the first time
do
{
if(firstPrompt) // do not display "Invalid filename" if this is the first prompt
{
firstPrompt = false; // disable first prompt
}
else
{
Console.WriteLine("Invalid filename."); // print "Invalid filename"
}
Console.WriteLine("Please enter the name of the first text file to be merged:"); // prompt for first filename
first = Console.ReadLine(); // read first filename
} while (first.Length > 0 && !File.Exists(first)); // check if first file exists
firstPrompt=true; // set first prompt to true for second filename input
do
{
if(firstPrompt) // do not display "Invalid filename" if this is the first prompt
{
firstPrompt = false; // disable first prompt
}
else
{
Console.WriteLine("Invalid filename."); // print "Invalid filename"
}
Console.WriteLine("Please enter the name of the second text file to be merged:"); // prompt for second filename
second = Console.ReadLine(); // read second filename
} while (second.Length > 0 && !File.Exists(second)); // check if first file exists
  
String merged=first.Substring(0,first.Length-4)+second.Substring(0,second.Length-4)+".txt"; // merged filename variable

StreamWriter sw=null; // Stream for writing into merged
StreamReader sr1=null; // Stream for reading from first
StreamReader sr2=null; // Stream for reading from second

bool success=false; // indicates whether merging was successful

int count=0; // counts the number of characters

try
{
sw=new StreamWriter(merged); // open writing stream for merged
sr1=new StreamReader(first); // open reading stream for first
sr2=new StreamReader(second); // open reading stream for second

String line=sr1.ReadLine(); // set line to first line in the first file
while(line!=null) // loop until end of file
{
sw.WriteLine(line); // write line into merged
count+=line.Length; // update total number of characters
line=sr1.ReadLine(); // read the next line
}
line=sr2.ReadLine(); // set line to first line in the second file
while(line!=null) // loop until end of file
{
sw.WriteLine(line); // write line into merged
count+=line.Length; // update total number of characters
line=sr2.ReadLine(); // read the next line
}
success=true; // merging successful
}
catch(Exception e)
{
Console.WriteLine(e.Message); // print error message
}
finally
{
if(sw!=null)
sw.Close(); // close stream for merged
if(sr1!=null)
sr1.Close(); // close stream for first
if(sr2!=null)
sr2.Close(); // close stream for second
if (success)
Console.WriteLine(merged + " was successfully saved. The document contains " + count + " characters."); // print successful message
}
}

static void Main(string[] args)
{
do
{
runOnce();
Console.WriteLine("Would you like to merge two more files? (y/n)"); // prompt for running again
char c = Console.ReadLine()[0]; // reading first character
if (c == 'n') // checking for 'no' option
break; // break the loop
} while (true); // run again
}
}
}