This problem involves two input files with the following format: FirstName, Last
ID: 3681713 • Letter: T
Question
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 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
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class ReadWriteFile {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner1 = null, scanner2 = null, scanner3 = null;
try {
scanner3 = new Scanner(System.in);
System.out.print("Enter first input file name:");
String input1 = scanner3.next();
System.out.print("Enter second input file name:");
String input2 = scanner3.next();
System.out.print("Enter output file name:");
String output = scanner3.next();
scanner1 = new Scanner(new File(input1));
scanner2 = new Scanner(new File(input2));
File outFile = new File(output);
// if file doesnt exists, then create it
if (!outFile.exists()) {
outFile.createNewFile();
}
FileWriter fw = new FileWriter(outFile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
List<Employee> employees = new ArrayList<Employee>();
while (scanner1.hasNext()) {
String line = scanner1.nextLine();
line = line.replaceAll(", ", ",");
String lineArr[] = line.split(" ");
for (int i = 0; i < lineArr.length; i++) {
String wordArr[] = lineArr[i].split(",");
if (wordArr.length == 4) {
String firstName, lastName, title;
double salary;
firstName = wordArr[0];
lastName = wordArr[1];
title = wordArr[2];
salary = Double.parseDouble(wordArr[3]);
if (firstName.equals("") || lastName.equals("")
|| title.equals("")) {
} else {
Employee employee = new Employee(firstName,
lastName, title, salary);
employees.add(employee);
}
}
}
}
while (scanner2.hasNext()) {
String line = scanner2.nextLine();
line = line.replaceAll(", ", ",");
String lineArr[] = line.split(" ");
for (int i = 0; i < lineArr.length; i++) {
String wordArr[] = lineArr[i].split(",");
if (wordArr.length == 4) {
String firstName, lastName, title;
double salary;
firstName = wordArr[0];
lastName = wordArr[1];
title = wordArr[2];
salary = Double.parseDouble(wordArr[3]);
if (firstName.equals("") || lastName.equals("")
|| title.equals("")) {
} else {
Employee employee = new Employee(firstName,
lastName, title, salary);
employees.add(employee);
}
}
}
}
for (int i = 0; i < employees.size(); i++)
bw.write(employees.get(i) + " ");
bw.close();
} catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("Exception! given file is not found");
} catch (StringIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println("Exception! invalid index of an array ");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
scanner1.close();
scanner2.close();
scanner3.close();
}
}
}
class Employee implements Comparable<Employee> {
String firstName, lastName, title;
double salary;
/**
* @param firstName
* @param lastName
* @param title
* @param salary
*/
public Employee(String firstName, String lastName, String title,
double salary) {
this.firstName = firstName;
this.lastName = lastName;
this.title = title;
this.salary = salary;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "" + firstName + "," + lastName + "," + title + "," + salary
+ "";
}
@Override
public int compareTo(Employee o) {
// TODO Auto-generated method stub
if (salary > o.salary)
return 1;
else
return 0;
}
}
input1.txt
Smith, John, Ensign, 22000 Doe, John, Ensign, 25000 Jones, , Crewman, 30000 Scott, Montgomery, Engineer, 60000 Kirk, James, Captain, 75000 Pike, Christopher, Admiral, ,
input2.txt
Chapel, Christine, Nurse, 55000 McCoy, Leonard, Doctor, 70000 Spock, ,Commander, 72000
output.txt
Smith,John,Ensign,22000.0 Doe,John,Ensign,25000.0 Chapel,Christine,Nurse,55000.0 Scott,Montgomery,Engineer,60000.0 McCoy,Leonard,Doctor,70000.0 Kirk,James,Captain,75000.0
OUTPUT:
Enter first input file name:input1.txt
Enter second input file name:input2.txt
Enter output file name:output.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.