Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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 arr=new ArrayList(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(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago