Question

In: Computer Science

This is JAVA PROGRAMMING Sort the contents of the two files in ascending order and combine...

This is JAVA PROGRAMMING

Sort the contents of the two files in ascending order and combine them into one new file (words.txt).
When comparing string order, you must use the compareTo method and make an exception.The source code below is a code that only combines files. Use the comparedTo method to sort the contents of the two files in ascending order.(ex. if(str1.compareTo(str2)<0) {})

<file1.txt>

at first  
castle  
consider  
considerable  
enlighten  
explain  
explanation  
female  

<file2.txt>

consideration  
considering that  
education  
educational  
endow  
inherit  

<code>

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab7_1 {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       fileMerge("file1.txt","file2.txt","words.txt");
   }
   private static void fileMerge(String filename1, String filename2, String filename3) {
       // TODO Auto-generated method stub
       Scanner str1=null;
       Scanner str2=null;
       PrintWriter output=null;
      
       try {
           str1 = new Scanner(new File(filename1));
               str2 = new Scanner(new File(filename2));
               output = new PrintWriter(new File(filename3));
              
               fileWriter(str1,output);
               fileWriter(str2,output);
              
          
      
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           System.err.println(e.getMessage());
           // e.printStackTrace();
      
       } finally {
           if (str1 != null)
               str1.close();
           if (str2 != null)
               str2.close();
           if (output != null)
               output.close();

       }
   }
       private static void fileWriter(Scanner str1, PrintWriter output) {
           // TODO Auto-generated method stub
           while(str1.hasNextLine()) {
               String str = str1.nextLine();
               output.println(str);
       }

   }

          
   }

Solutions

Expert Solution

Code For Above Problem:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab7_1 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub

                fileMerge("file1.txt", "file2.txt", "words.txt");
        }

        private static void fileMerge(String filename1, String filename2, String filename3) {
                // TODO Auto-generated method stub
                Scanner str1 = null;
                Scanner str2 = null;
                PrintWriter output = null;

                try {
                        str1 = new Scanner(new File(filename1));
                        str2 = new Scanner(new File(filename2));
                        output = new PrintWriter(new File(filename3));

                        fileWriter(str1, str2, output);

                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        System.err.println(e.getMessage());
                        // e.printStackTrace();

                } finally {
                        if (str1 != null)
                                str1.close();
                        if (str2 != null)
                                str2.close();
                        if (output != null)
                                output.close();

                }
        }

        /**
         * 
         * @param str1   Scanner for file1.txt
         * @param str2   Scanner for file2.txt
         * @param output PrintWriter for words.txt file
         */
        private static void fileWriter(Scanner str1, Scanner str2, PrintWriter output) {
                // TODO Auto-generated method stub
                String s1 = null;// get the words from file1
                String s2 = null;// get the words from file2
                if (str1.hasNextLine()) {
                        s1 = str1.nextLine();
                }
                if (str2.hasNextLine()) {
                        s2 = str2.nextLine();
                }

                while (s1 != null && s2 != null) {
                        if (s1.compareTo(s2) < 0) {
                                output.println(s1);
                                if (str1.hasNextLine())
                                        s1 = str1.nextLine();
                                else {
                                        s1 = null;
                                }
                        } else if (s1.compareTo(s2) > 0) {
                                output.println(s2);
                                if (str2.hasNextLine())
                                        s2 = str2.nextLine();
                                else {
                                        s2 = null;
                                }
                        }
                }

                // for remaining words in File1
                while (s1 != null) {
                        output.println(s1);
                        if (str1.hasNextLine())
                                s1 = str1.nextLine();
                        else {
                                s1 = null;
                        }
                }

                // for remaining words in File2
                while (s2 != null) {
                        output.println(s2);
                        if (str2.hasNextLine())
                                s2 = str1.nextLine();
                        else {
                                s2 = null;
                        }
                }

        }

}

<file1.txt>

at first  
castle  
consider  
considerable  
enlighten  
explain  
explanation  
female  

<file2.txt>

consideration  
considering that  
education  
educational  
endow  
inherit  

<Words.txt>

at first
castle
consider
considerable
consideration
considering that
education
educational
endow
enlighten
explain
explanation
female
inherit

Image Of <words.txt>:(Result After running Above Code)


Related Solutions

2. Combine multiple files Use Python programming to combine two text files: customer-status.txt and sales.txt Data...
2. Combine multiple files Use Python programming to combine two text files: customer-status.txt and sales.txt Data columns in customer-status.txt (separated by comma): Account Number, Name, Status 527099,Sanford and Sons,bronze Data columns in sales.txt (separated by comma): Account Number, Name, SKU, Quantity, Unit Price, Ext Price, Date 163416,Purdy-Kunde,S1-30248,19,65.03,1235.57,2014-03-01 16:07:40 527099,Sanford and Sons,S2-82423,3,76.21,228.63,2014-03-01 17:18:01 After you combine, you will see the following: 527099,Sanford and Sons,S2-82423,3,76.21,228.63,2014-03-01 17:18:01,bronze
I have multiple .txt files in a folder, and i want to combine all their contents...
I have multiple .txt files in a folder, and i want to combine all their contents into a single .txt file How do I go about such an operation in Python?
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show...
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show all steps. 7          11        2          9          5          14 Q4. State the number of comparisons for each pass. Pass # comparisons 1st 2nd 3rd 4th 5th
Excel sorting In excel how do you sort a column of months in ascending order and...
Excel sorting In excel how do you sort a column of months in ascending order and not alphabetical when the month is in word format?
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
Hello i need someone to implement in JAVA a radix sort algorithm forsorting strings in ascending...
Hello i need someone to implement in JAVA a radix sort algorithm forsorting strings in ascending order. The input to your algorithm should be a (multi)set S = [S1, S2, . . . , Sn] of strings each of which is of length m over the English alphabet [A…Z, a…z]. The output should be the set sorted in ascending lexicographical order. Number of strings is >= 100 and number of characters >= 50 i need the answer fast please
C programming language only! a file is given that has comma-separated integers. the files contents are...
C programming language only! a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1... Create a function that takes as an input a filename and returns an array containing the list of integers in the file
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Display nonduplicate names in ascending order) Given one or more text files, each representing a day’s...
Display nonduplicate names in ascending order) Given one or more text files, each representing a day’s attendance in a course and containing the names of the students who attended the course on that particular day, write a program that displays, in ascending order, the names of those students who have attended at least one day of the course. The text file(s) is/are passed as command-line argument(s). USe language java, ide netbeans
Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.
Programming Language : JavaModify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.After it has been sorted in descending order, go through all the items in the array and make sure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT