Question

Create a MergeFiles application that merges the integers ordered from low to high in two text...

Create a MergeFiles application that merges the integers ordered from low to high in two text data files into a third text data file, maintaining the order from low to high (no post merge sorts!). For example, the two files of integers could contain:

File 1: 12 23 34 45 56 67 69 123 133

File 2: 4 5 10 20 35 44 100 130 150 160 180

The application should not use an array or ArrayList to temporarily store the numbers, but should merge the two files taking one element at a time from each. After MergeFiles runs for this example, the third file should contain:

4 5 10 12 20 23 34 35 44 45 56 67 69 100 123 130 133 150 160 180

Create the two input files using a text editor with the numbers of your choice. Be sure to employ proper exception handling for the file I/O. Use at least one try-catch statement that properly handles exceptions.

Homework Answers

Answer #1

HI, Pease find my implementation.

Please let me know in case of any issue.

import java.io.File;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class MergeFiles {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter first input file name: ");

       String firstFile = sc.next();

       System.out.print("Enter second input file name: ");

       String secondFile = sc.next();

       System.out.print("Enter output file name: ");

       String outputFile = sc.next();

       try{

           // opening both input file

           Scanner readFileOne = new Scanner(new File(firstFile));

           Scanner readFileTwo = new Scanner(new File(secondFile));

           // Opening file writer

           PrintWriter writeFile = new PrintWriter(new File(outputFile));

           // creating two array list

           ArrayList<Integer> firstArr = new ArrayList<>();

           ArrayList<Integer> secondArr = new ArrayList<>();

           // reading first file

           while(readFileOne.hasNextInt()){

               firstArr.add(readFileOne.nextInt());

           }

           // reading second file

           while(readFileTwo.hasNextInt()){

               secondArr.add(readFileTwo.nextInt());

           }

          

           // printing both list

           System.out.println(firstArr);

           System.out.println(secondArr);

          

           // merging two array and writing to file

           int i=0, j=0;

           while(i<firstArr.size() && j<secondArr.size()){

               if(firstArr.get(i) <= secondArr.get(j)){

                   writeFile.print(firstArr.get(i)+" ");

                   i++;

               }else{

                   writeFile.print(secondArr.get(j)+" ");

                   j++;

               }

           }

          

           // writing remaining entries of firtArr or secondArr

           while( i < firstArr.size()){

               writeFile.print(firstArr.get(i)+" ");

               i++;

           }

          

           while( j < secondArr.size()){

               writeFile.print(secondArr.get(j)+" ");

               j++;

           }

          

           // closing all files

           sc.close();

           readFileOne.close();

           readFileTwo.close();

           writeFile.close();

          

           System.out.println("File Written Successfully!!!");

       }catch(Exception e){

           System.out.println(e.getMessage());

       }

   }

}

####### file1.txt #######

12 23 34 45 56 67 69 123 133

########## file2.txt #######

4 5 10 20 35 44 100 130 150 160 180

########## mergedfile.txt $$$$$$$$$$$$$
4 5 10 12 20 23 34 35 44 45 56 67 69 100 123 130 133 150 160 180

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You want to see if there is a statistical difference between the two groups. Run descriptives...
You want to see if there is a statistical difference between the two groups. Run descriptives and a two-tailed, two sample assuming equal variance t-test. Here's your data: Weight of Apples in Grams Apple ID Farm A Farm B 1 131 151 2 147 159 3 134 162 4 134 158 5 136 159 6 137 160 7 140 150 8 134 160 9 136 160 10 133 160 11 134 160 12 132 158 13 139 162 14 136...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Your IT department provided you data on patients that received ER services, their GHHS, and their...
Your IT department provided you data on patients that received ER services, their GHHS, and their recovery time. Prepare a report to share with the owners of the facility that will help you make informed decisions about how long you can expect a patients’ recovery time would be based on their GHHS. Based on your findings provide recommendations on your plan moving forward to improve the functioning of your facilities in generating revenue. Prepare a report that addresses each of...
Pat.# Before After 1 195 125 To the left is 2 datasets measured for a sample...
Pat.# Before After 1 195 125 To the left is 2 datasets measured for a sample of 107 patients with the high cholesterol level before and after taking medication. Obtain the following for both sets: 2 208 164 3 254 152 1- the mean, mode and median 4 226 144 5 290 212 2- the variance and standard deviation 6 239 171 7 216 164 3- First, second and third quartiles 8 286 200 9 243 190 10 217 130...
A physiological experiment was concluded to study the effect of various factors on a pulse rate....
A physiological experiment was concluded to study the effect of various factors on a pulse rate. The participants took their own pulse. They then were asked to flip a coin. If their coin came up head, they were to run in place for 1 minute. Then everyone took their own pulse again. The dataset pulse.txt, available from Moodle, contains the following variables: ROW- id numbers, from 1 to 92; PULSE1- first pulse rate; PULSE2- second pulse rate; RAN-1=ran in place,...
A loan made on March 14 is due September 13 of the following year. Find the...
A loan made on March 14 is due September 13 of the following year. Find the exact time for the loan in a​ non-leap year and a leap year. days. The exact time in a​ non-leap year is ? days The exact time in a leap year is ? days. Data Table Sequential Numbers for Dates of the Year Day of Month Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec. 1 1 32 60 91 121...
The Red Lobster Restaurant chain conducts regular surveys of its customers to monitor the performance of...
The Red Lobster Restaurant chain conducts regular surveys of its customers to monitor the performance of individual restaurants. One of the questions asks customers to rate the overall quality of their last visit. the listed responses are Poor 1, Fair 2, Good 3, Very Good 4, and Excellent 5. The survey also asks respondents whether their children accompanied them (1=yes) and (2=no). Graphically depict these data and describe your findings. Customer Rate Children 1 4 1 2 3 2 3...