Textbook: Programming Logic and Design, 8th Edition, Joyce Farrell. Looking for
ID: 663152 • Letter: T
Question
Textbook: Programming Logic and Design, 8th Edition, Joyce Farrell.
Looking for solution to Chapter 7, question 3PE.
Question is:
Laramie Park District has files of participants in it summer and winter programms this year. Each file is in participant ID number order and contains additional fields for first name, last name, age, and class take (for example, Beginning Swimming).
a. Design the logic for a program that merges the files for summer and winter programs to create a list of the first and last names of all participants for the year in ID number order.
b. Modify the program so that if a participant has more than one record, the participant's ID number and name are output only once.
c. Modify the program so that if a participant has more than one record, the ID number and name are output only once along with a count of the total number of classes the participant has taken.
Explanation / Answer
I have written your requirement in java first of all create two file summer.txt and winter.txt and give path name for mrerge file where you want to store data.
Below program takes two files and merges two files data into one file with unique data only
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
public class demo {
public static void main(String[] args) {
String summer = "****************/summer.txt";
String winter ="**********************/winter.txt";
String mergedFilePath = "***************/merge.txt";
File[] files = new File[2];
files[0] = new File(summer);
files[1] = new File(winter);
File summerwinter = new File(mergedFilePath);
//summerwinter.deleteOnExit();
mergeFiles(files, summerwinter);
readFileData(summerwinter);
}
public static void readFileData(File summerwinter) {
try {
Set<String> storeWordList = new HashSet<String>();
FileInputStream fstream = new FileInputStream(summerwinter);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
storeWordList.add(strLine);
}
//Close the input stream
in.close();
for (String unique : storeWordList) {
System.out.println(unique);
}
for (int i=1;i<storeWordList.size();i++) {
System.out.println(storeWordList);
}
System.out.println("Your file has been written");
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static void mergeFiles(File[] files, File mergedFile) {
FileWriter fstream = null;
BufferedWriter out = null;
try {
fstream = new FileWriter(mergedFile, true);
out = new BufferedWriter(fstream);
out.write("ID FIRSTNAME LASTNAME AGE CLASS");
out.write(" ");
} catch (IOException e1) {
e1.printStackTrace();
}
for (File f : files) {
System.out.println("merging: " + f.getName());
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
in.readLine();
while ((aLine = in.readLine()) != null) {
out.write(aLine);
out.newLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.