Java Help: I Need help turning this in to a BufferedReader. public class Employe
ID: 3780033 • Letter: J
Question
Java Help: I Need help turning this in to a BufferedReader.
public class EmployeeDAO {
private final String fileName = "person_data.txt";
public Employee getEmployee(String ID){
File inputFile = new File(fileName);
try {
Scanner in = new Scanner(inputFile);
while(in.hasNext()){
String line = in.nextLine();
String[] lineArray = line.split("[|]");
String empID = lineArray[0];
if(ID == null ? empID == null : ID.equals(empID)){
Employee employee = new Employee();
employee.setEmployeeId(Integer.parseInt(lineArray[0]));
employee.setFirstName(lineArray[1]);
employee.setLastName(lineArray[2]);
employee.setPassword(lineArray[3]);
employee.setRole(lineArray[4]);
in.close();
return employee;
}
}
} catch (FileNotFoundException ex) {
System.out.println(ex)
}
return null;
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Employee {
private final String fileName = "person_data.txt";
public Employee getEmployee(String ID){
/*
Please add Below code Start Added ******************
*/
BufferedReader reader = null;//First Intialise buffer reader as null
String line="";//Use String line=null
try {
File file = new File(fileName);//Create File Object
reader = new BufferedReader(new FileReader(file));//Assign to BufferReader Object
while ((line = reader.readLine()) != null) { //Reading the line
line = reader.readLine();//Store in String line by line
/*
****************** Please add above code End please check in catch block
*/
String[] lineArray = line.split("[|]");
String empID = lineArray[0];
if(ID == null ? empID == null : ID.equals(empID)){
Employee employee = new Employee();
employee.setEmployeeId(Integer.parseInt(lineArray[0]));
employee.setFirstName(lineArray[1]);
employee.setLastName(lineArray[2]);
employee.setPassword(lineArray[3]);
employee.setRole(lineArray[4]);
in.close();
return employee;
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace(); //Please change System.out.print(""); to ex.printStackTrace();
}
return null;
}
}
============================
Once go through the Please find my comments using Buffer Reader
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.