Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is my code: import java.io.*; import java.util.*; public class MergingFiles { /********************************************************** * Program Description: This method will join two files together * to

Here is my code:

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

public class MergingFiles { /********************************************************** * Program Description: This method will join two files together * to a third file by inputting the names of the * two files and join them together by * alphabetical order. * * Psedocode: * BEGIN main * TRY * Input file 1 name or quit * WHILE (user didn't quit) * Input file 2 name * Open files * WHILE (two files aren't empty) * Set the next name of first file to name 1 * Set the next name of second file to name 2 * IF (name1 is alphabetically before name2) * Print name1 to output file * ELSE * Print name2 to output file * END IF * END WHILE * WHILE (theres names in first file) * Print name 1 to output file * Set the next name of first file to name 1 * END WHILE * WHILE (theres names in second file) * Print name 2 to output file * Set the next name of second file to name 2 * END WHILE * Disp successful merge msg * Input first file name or quit * END WHILE * Close output file * END TRY * CATCH (IOexception) * Disp error msg * END CATCH * END main *********************************************************/ public static void main(String[] args) throws FileNotFoundException { // Constants final String OUTPUT_FILE = "output.txt"; final String QUIT = "-1";

// Variables 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 Scanner fileScan1; // Object to read lines from the first input file Scanner fileScan2; // Object to read lines from the second input file Scanner scan = new Scanner(System.in); // Object to get data from user PrintWriter output = new PrintWriter(OUTPUT_FILE); // Object to output text into output file /************************************************/

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

// WHILE (user didn't quit) while (!firstFile.equals(QUIT)) { // Input name of second file System.out.print("\t\t\tInput second file name : "); secondFile = scan.nextLine();

// Open files fileScan1 = new Scanner(new File (firstFile)); fileScan2 = new Scanner(new File (secondFile));

// WHILE (The two files aren't empty) while (fileScan1.hasNextLine() && fileScan2.hasNextLine()) { // Set next line of first file to the name 1 name1 = fileScan1.nextLine();

// Set next line of second file to name 2 name2 = fileScan2.nextLine();

// IF (name1 is alphabetically before name2) if (name1.compareTo(name2) < 0) { // Print name1 to output file output.println(name1); }

// ELSE (name2 is alphabetically before name1) else { output.println(name2);

} // END IF

}// END WHILE

// WHILE (theres names in file 1) while (fileScan1.hasNextLine()) { // Print name output.println(name1);

// Print next name name1 = fileScan1.nextLine();

}// END WHILE

// WHILE (theres names in file 2) while (fileScan2.hasNextLine()) { // Print name output.println(name2);

// Print next name name2 = fileScan2.nextLine();

}// END WHILE

// Disp successful merge msg System.out.println(" \t\t\tMerge Successful!");

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

}// END WHILE

//close the output file for writing output.close(); }// END TRY

// CATCH (IOexception) catch (IOException io) { // Disp error msg System.out.println("\t\t\tError reading file");

}// END CATCH

}// END main

}// END MergingFiles

It is supposed to take two files that already have names in them that are sorted alphabetically and merge those two files into an output file merged alphabetically. The problem is it doesn't do that and only outputs some of the results if someone could tell me exactly why that issue is happening and show me the fixed version that would be greatly appreciated.

The official 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 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.

PLEASE DO NOT USE ANY BREAK STATEMENTS, TERNARY TYPES, GENERIC TYPES, ARRAYS, AND FINNALY CLAUSES! Please make it look simmilar to my code!

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

More Books

Students also viewed these Databases questions