Create a text file that contains ten Person objects where each object is represe
ID: 3826224 • Letter: C
Question
Create a text file that contains ten Person objects where each object is represented on a separate line with each attribute separated by a comma. Create a method that reads from the file, creates one Person object per line, and stores the object in an ArrayList object. Write the stored sorted objects by last name and display last name and first name to the console. For example, Salisbury, Robert.
The teaacher wants us to create this text file in the program itself by using file input commands within the program. The characateristics of the text file is from the previous person file we used before, which are these characterisitics: first name, last name, age, gender , social securtiy number. He wants the output to console to display last name first, then first name. For that to happen, he wants the arraylist to do a sort, so as to display the output correctly. Also, when we create the file, call it persons.text and save it to : c:/temp. file exceptions should also be in this program. Can you help me?
Explanation / Answer
import java.io.*;
import java.util.*;
//Person class stores information about each person in a object
class Person
{
private String firstName;
private String lastName;
private int age;
private String gender;
private int social_security_no;
public Person()
{
firstName="";
lastName="";
age=0;
gender="";
social_security_no=0;
}
public Person(String f,String l,int a,String g,int s)
{
firstName=f;
lastName=l;
age=a;
gender=g;
social_security_no=s;
}
public void display()
{
System.out.println("First name");
}
//Returns last name and first name
public String lastFirst()
{
return lastName+" "+firstName;
}
}
public class PersonHandle
{
public static void main(String args[])
{
//Reading 10 objects from persons.txt file which contains data in specified format
ArrayList<Person> arr=new ArrayList<Person>(10);
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader("person.txt");
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader("person.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String a[]=sCurrentLine.split(",");
//creating Person object and adding it into arr ArrayList object
Person p=new Person(a[0],a[1],Integer.parseInt(a[2]),a[3],Integer.parseInt(a[4]));
arr.add(p);
//System.out.println(sCurrentLine);
}
//System.out.println(arr);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
try
{
//Writing last name and first name of each person in ArrayList into persons.txt file in c:/temp directory
File f=new File("persons.txt");
// BufferedWriter bf=new BufferedWriter(f);
PrintWriter pw=new PrintWriter(f);
Iterator it=arr.iterator();
while(it.hasNext())
{
String g=((Person)it.next()).lastFirst();
System.out.println(g);
pw.write(g);
pw.write(" ");
}
pw.flush();
pw.close();
//pw.write("Hello world");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Persons.txt in C:/temp
frd agv
gfd fdsf
dt df
person.txt
agv,frd,12,male,432
fdsf,gfd,43,female,4543
df,dt,4,male,653
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.