Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. This problem involves two input files with the following format: FirstName, L

ID: 3681635 • Letter: 5

Question

5. This problem involves two input files with the following format: FirstName, LastName, Title, Salary

The entries within each the files will already be sorted in ascending order by salary. Write a method in JAVA that produces a new file containing the entries from both input files, still sorted in ascending order by salary. If any entry is missing a field, you should ignore it (i.e. do not write that entry to the combined file). Prompt the user for the names of the input files and the output file. Handle all exceptions appropriately, including displaying “friendly” error messages to the user rather than the stack trace.

Assuming the first input file has the contents:

Smith, John, Ensign, 22000

Doe, John, Ensign, 25000

Jones, , Crewman, 30000

Scott, Montgomery, Engineer, 60000

Kirk, James, Captain, 75000

Pike, Christopher, Admiral,

, and the second input file has the contents:

Chapel, Christine, Nurse, 55000

McCoy, Leonard, Doctor, 70000

Spock, ,Commander, 72000

the output file should have the contents:

Smith, John, Ensign, 22000

Doe, John, Ensign, 25000

Chapel, Christine, Nurse, 55000

Scott, Montgomery, Engineer, 60000

McCoy, Leonard, Doctor, 70000

Kirk, James, Captain, 75000

Explanation / Answer

I have used Person class to store entries of input files.

################ Person.java #########################

public class Person implements Comparable<Person> {
  
   private String firstName;
   private String lastName;
   private int salary;
   private String language;
  
   public Person(String firstName, String lastName, int salary, String language) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.salary = salary;
       this.language = language;
   }

   public String getFirstName() {
       return firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public int getSalary() {
       return salary;
   }

   public String getLanguage() {
       return language;
   }

   @Override
   public int compareTo(Person o) {
       if(this.salary < o.salary)
           return -1;
       else if(this.salary == o.salary)
           return 0;
       else
           return 1;
   }
  
   @Override
   public String toString() {
       return firstName+", "+lastName+", "+language+", "+salary;
   }

}

################## PersonRecord.java ##############################

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class PersonRecords {
  
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter first input filename: ");
       String firstFileName = sc.next();
       System.out.print("Enter second input filename: ");
       String secondFileName = sc.next();
       System.out.print("Enter output input filename: ");
       String outputFile = sc.next();
      
       ArrayList<Person> list = new ArrayList<Person>();
      
       try {
           FileReader fr1 = new FileReader(firstFileName);
           FileReader fr2 = new FileReader(secondFileName);
          
           BufferedReader br1 = new BufferedReader(fr1);
           BufferedReader br2 = new BufferedReader(fr2);
          
           FileWriter fw = new FileWriter(outputFile);
           BufferedWriter bw = new BufferedWriter(fw);
          
           // reading first input file
           String line = null;
          
           while((line = br1.readLine())!=null){
              
               String s[] = line.replaceAll("^[,\s]+", "").split("[,\s]+");
              
               if(s.length==4){// if line has four entries
                  
                   Person p = new Person(s[0].trim(), s[1].trim(), Integer.parseInt(s[3].trim()), s[2].trim());
                   list.add(p);
               }
           }
          
           br1.close();
           fr1.close();
           System.out.println("------------------------------");
           //reading second input file
           while((line = br2.readLine())!=null){
              
               String s[] = line.replaceAll("^[,\s]+", "").split("[,\s]+");
              
               if(s.length==4){// if line has four entries
                  
                   Person p = new Person(s[0].trim(), s[1].trim(), Integer.parseInt(s[3].trim()), s[2].trim());
                   list.add(p);
               }
           }
          
           br2.close();
           fr2.close();
       // sorting list
       Collections.sort(list);
          
       // writing to output file
       for(int i=0; i<list.size(); i++){
           Person p = list.get(i);
           bw.write(p.toString());
           bw.newLine();
       }
      
       bw.close();
       fw.close();
      
       System.out.println("Successfully writen to file");
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
   }

}

/*

Input Files:

firstFile.txt:

Smith, John, Ensign, 22000
Doe, John, Ensign, 25000
Jones, , Crewman, 30000
Scott, Montgomery, Engineer, 60000
Kirk, James, Captain, 75000
Pike, Christopher, Admiral,,

secondFIle.txt:

Chapel, Christine, Nurse, 55000
McCoy, Leonard, Doctor, 70000
Spock, ,Commander, 72000

Output:

outputFile.txt:

Smith, John, Ensign, 22000
Doe, John, Ensign, 25000
Chapel, Christine, Nurse, 55000
Scott, Montgomery, Engineer, 60000
McCoy, Leonard, Doctor, 70000
Kirk, James, Captain, 75000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote