5.a. Create a program that accepts a series of employee ID number, first names,
ID: 3764034 • Letter: 5
Question
5.a. Create a program that accepts a series of employee ID number, first names, and last names from the keyboard and saves the data to a file. Save the program as WriteEmployeeList.java. When you execute the program, be sure to enter multiple records that have the same first name because you will search for repeated first names in part d of this exercise.
5.b. Write an application that reads the file created by the WriteEmployeeList application and displays the records. Save the file as DisplaySavedEmployeeList.java.
Explanation / Answer
import java.io.*;
import java.util.*;
public class WriteEmployeeList{
public static void main(String[] args)
{
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new FileWriter("records.txt"));
Scanner s=new Scanner(System.in);
String x="y";
while(x.equals("Y")||x.equals("y")){
System.out.println("Enter Employeeid, First name, last name");
int id=s.nextInt();
String fname=s.next();
String lname=s.next();
String data=id+ " "+fname+" "+lname;
System.out.println(data);
bw.write(data);
System.out.println("Want more(Y/N)");
x=s.next();
}
bw.close();
}
catch ( Exception e) {
}
}
}
****************************************************************
import java.io.*;
import java.util.*;
public class DisplaySavedEmployeeList{
public static void main(String[] args)
{
BufferedReader br=null;
try{
br=new BufferedReader(new FileReader("records.txt"));
System.out.println("EmpId FirstName LastName");
String output=br.readLine();
while(output!=null){
System.out.println(output);
output=br.readLine();
}
br.close();
}
catch ( Exception e) {
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.