Java The Person class contains the following constructor: /** constructs a perso
ID: 3883967 • Letter: J
Question
Java
The Person class contains the following constructor:
/** constructs a person based on name and dob. but if the name or dob are unreasonable, it throws a BadPersonException. */
Person(String name, String dob) throws BadPersonException
Complete the following static method of some class X:
/** pre: f's file is readable and contains at least 2 hunks on each line
post: returns persons constructed using the first 2 hunks of each line as name and dob
prints lines that could not be used to construct a person. */
static ArrayList getPeople(File f){
Explanation / Answer
static ArrayList getPeople(File f) throws FileNotFoundException, IOException{
BufferedReader br = new BufferedReader(new FileReader(f));
String str;
//List to store the persons
ArrayList<Person> pList = new ArrayList();
//Read the file
while((str=br.readLine())!=null){
//Parse name and dob
String details[] = str.trim().split(" ");
String name = details[0];
String dob = details[1];
try {
//Create an object
Person person = new Person(name,dob);
//add the object to the list
pList.add(person);
} catch (BadPersonException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
return pList;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.