Question
Do not copy the answer from other questions. It is not the correct answer. I need the code for 3 merges not 2. The code
Do not copy the answer from other questions. It is not the correct answer. I need the code for 3 merges not 2. The code below is for merging two sorted files. Modify it so it can merge 3 sorted files. Thank You.
import java.util.*; import java.io.*; public class XXXXXH1 { // print k integers per line int k = 20; //use a short word for System.out to save typing PrintStream prt = System.out; // print file contents private void prtfile(String fn){ //declare variables int i=0, x; prt.printf(" %s:", fn); try{ Scanner inf = new Scanner(new File(fn)); while (inf.hasNext()) { //read an input from inf x = inf.nextInt(); prt.printf("%3d ", x); i++; if(i % k == 0) prt.printf(" %s:", fn); } // endwhile prt.printf("(count=%d)", i); // close file inf.close(); } catch (Exception e){ prt.printf(" Ooops! Read Error:");} } // end prtfile // complete following method private void merge(String fn1, String fn2, String fn3, String fn4){ int x, y; try{ // merge two sorted integer files fn1 and fn1 into fn3 prtfile(fn1); // print first sorted file prtfile(fn2); // print second sorted file prt.printf(" \tS T A R T * M E R G I N G "); // open input files F1 & F2 & F3 Scanner F1 = new Scanner(new File(fn1)); Scanner F2 = new Scanner(new File(fn2)); Scanner F3 = new Scanner(new File(fn3)); // open output file F3 PrintWriter F4 = new PrintWriter(new File(fn3)); // read an input from file F1 x = F1.nextInt(); // read an input from file F2 y = F2.nextInt(); // read an input from file F3 z = F3.nextInt(); // complete the rest while (F1.hasNext() && F2.hasNext() && F3.hasNext()) { if (x=y)> F4.printf("%4d", x); x = F1.nextInt(); } else { F3.printf("%4d", y); y = F2.nextInt(); } } if (!F1.hasNext()) { while (F2.hasNext()) { F3.printf("%4d", y); y = F2.nextInt(); } } else { while (F1.hasNext()) { F3.printf("%4d", x); x = F1.nextInt(); } } if (x => F3.printf("%4d", x); F3.printf("%4d", y); } else{ F3.printf("%4d", y); F3.printf("%4d", x); } // complete the rest // close all files F1.close(); F2.close(); F3.close(); F4.close(); } catch (Exception e){prt.printf(" Ooops! I/O Error:");} prtfile(fn3); // print final sorted file prt.printf(" \tE N D * M E R G I N G"); } // end merge public static void main(String[] args) throws Exception{ int cnt = args.length; // get no. of arguments if (cnt == 4){ String f1 = args[0]; // get first file name String f2 = args[1]; // get second file name String f3 = args[2]; // get third file name String f4 = args[3]; // get fourth file name //System.out.printf(" f1=%s, f2=%s, f3=%s, cnt=%d", f1, f2, f3, cnt); // create an instance of xxxxxH1 class XXXXXH1 srt = new XXXXXH1(); srt.merge(f1, f2, f3); //MAKE SURE TO WRITE YOUR NAME IN NEXT LINE System.out.printf(" \tAuthor: XXXXX XXXXX Date: " + java.time.LocalDate.now()); } else System.out.printf(" \tOOOPS Invalid No. of aguments!" + " \tTO Execute: java xxxxxH1 f1.txt f2.txt f3.txt"); } // end main } // end xxxxxH1
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