Question: Design and write an application that will merge two files that are already sorted alphabetically. The merged file will also be sorted alphabetically. The input

Design and write an application that will merge two files that are already sorted alphabetically. The merged file will also be sorted alphabetically. The input files will contain 1 full name per file. On each line will be the last, first name with a space between the comma and the first name. All file names will be input from the user. You should recover from a FileNotFoundException and allow the user the opportunity to enter a valid file name. All files will have a .txt extension. I should be able to do this for multiple sets of files (use a sentinel value), or no sets of files.

Here is the code I have so far:

import java.io.*; import java.util.*;

public class MergingFiles { /********************************************************** * Psedocode: * BEGIN JoiningTwoFiles * TRY * Input name of first file * Input name of second file * Open both files for writing * WHILE (both files have lines remaining) * Init count = 0 * Init length = 0 * Init name1 to an empty string * Init name2 to an empty string * Read next line in first file and store line in name1 * Read next line in second file and store line in name2 * IF (name1 is the same as name2) * Send both names to output file * ELSE * Set if outputted equal to false * WHILE (a name has not been sent to output file) * Init char at name1 is char at count position * Init char at name2 is char at count position * IF (char in name1 is alphabetically before first char in name2) * Send name1 to output file * Set if outputted equal to true * ELSE IF (char in name2 is alphabetically before first char in name1) * Send name2 to output file * Set if outputted equal to true * ELSE * count++ * END IF * END WHILE * END IF * END WHILE * END TRY * CATCH (IOexception) * Disp error msg * END CATCH * Close output file * END JoiningTwoFiles * @throws FileNotFoundException **********************************************************/ public static void main(String[] args) throws FileNotFoundException { // Constants final String OUTPUT_FILE = "output.txt"; final String QUIT = "-1";

// Variables int count; // Count of the letters in the name String firstFile; // The name of the first input file String secondFile; // The name of the second input file String name1; // The name on first file String name2; // The name on second file char charName1; // Characters in name for first file char charName2; // Characters in name for second file boolean ifOutputted; // To see if we have outputted a name Scanner fileScan1; // Object to read lines from the first input file Scanner fileScan2; // Object to read lines from the second input file

PrintWriter output = new PrintWriter(OUTPUT_FILE); Scanner scan = new Scanner(System.in); /******************************/

// TRY try { // Input name of first file System.out.println(" \t\t\tInput first file name or enter -1 to quit: "); firstFile = scan.nextLine();

while (!firstFile.equals(QUIT)) { // Input name of second file System.out.println("\t\t\tInput second file name:"); secondFile = scan.nextLine();

// Open the first file for writing fileScan1 = new Scanner(new File (firstFile));

// Open the second file for writing fileScan2 = new Scanner(new File (secondFile));

// WHILE (both files have lines remaining) while (fileScan1.hasNext() && fileScan2.hasNext()) { // Init count = 0 count = 0;

// Init name1 to empty string name1 = ""; // Init name2 to empty string name2 = "";

// Read next line in first file and store line in name1 name1 = fileScan1.nextLine();

// Read next line in second file and store line in name2 name2 = fileScan2.nextLine();

// IF (name1 is the same as name2) if (name1.equals(name2)) { // Send both names to output file output.println(name1 + " "); output.println(name2 + " "); }

// ELSE (the two names aren't the same) else { // Set ifOutputted = false ifOutputted = false;

// WHILE (a name has not been sent to output file) while (!ifOutputted) {

// Init char at name1 is char at count position for name1 charName1 = name1.charAt(count);

// Init char at name2 is char at count position for name2 charName2 = name2.charAt(count);

// IF (char in name1 is alphabetically before first char in name2) if (charName1 > charName2) { // Send name1 to output file output.println(name1 + " ");

// Set if outputted equal to true ifOutputted = true; }

else if (charName2 > charName1) { // Send name2 to output file output.println(name2);

// Set if outputted equal to true ifOutputted = true; }

else { // Increment count count++; }// END IF

}// END WHILE }// END IF

}// END WHILE

//close the files for writing output.close(); fileScan1.close(); fileScan2.close();

// Input name of first file System.out.println(" \t\t\tInput first file name or enter -1 to quit: "); firstFile = scan.nextLine(); }// END WHILE

}// END TRY

// CATCH (IOexception) catch (IOException io) { System.out.println("Error reading/writing file: " + io.getMessage()); }// END CATCH

}

} The issue is I am not getting an output, if anyone could help me out that would be greatly appreciated

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!