Create a text file that contains ten Person objects where each object is represe
ID: 3826655 • 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?
** I got a response from an expert, and everything was ok, except that the teacher wanted us to have the text file of the persons created in the program. The "persons.text" file is to be created by an arraylist in the program, and then it is to be read in the program. The last expert had it to where it was being read from , but the program itself did not create the text file. Here is what the last expert sent me. Can someone help me get it completed?
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();
}
}
}
Explanation / Answer
HI, I have modified the program so that it will write sorted data in temp folder.
Please let me know in case of any issue.
//Person class stores information about each person in a object
public 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 String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public int getSocial_security_no() {
return social_security_no;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setSocial_security_no(int social_security_no) {
this.social_security_no = social_security_no;
}
}
##############
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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("c:\temp\persons.txt");
// BufferedWriter bf=new BufferedWriter(f);
PrintWriter pw=new PrintWriter(f);
//Iterator it=arr.iterator();
for(Person p : arr)
{
//String g=((Person)it.next()).lastFirst();
System.out.println(p.lastFirst());
// writing in file
pw.write(p.getFirstName()+","+p.getLastName()+","+p.getAge()+","+p.getGender()+
","+p.getSocial_security_no());
pw.write(" ");
}
pw.flush();
pw.close();
//pw.write("Hello world");
}
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.