Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a ArrayList of Person objects. Each object contains fullName, phoneNumber

ID: 647904 • Letter: I

Question

I have a ArrayList of Person objects. Each object contains fullName, phoneNumber, and email.

I have to  open a text file, loop over the list of persons writing each one into the file as separate lines. (full name, phone number, and email all separated by commas ( Tip: Use toString() on each person).

How can I do this using FileWriter?

In the file they need to be presented like this below.

fullName, phoneNumber, email
fullName, phoneNumber, email
fullName, phoneNumber, email

Explanation / Answer

public class Person implements Comparable { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public int getAge() { return this.age; } public String getName() { return this.name; } @Override public String toString() { return ""; } @Override public int compareTo(Person per) { if(this.age == per.age) return 0; else return this.age > per.age ? 1 : -1; } public static void main(String[] args) { Person e1 = new Person("Adam", 45); Person e2 = new Person("Steve", 60); int retval = e1.compareTo(e2); switch(retval) { case -1: { System.out.println("The " + e2.getName() + " is older!"); break; } case 1: { System.out.println("The " + e1.getName() + " is older!"); break; } default: System.out.println("The two persons are of the same age!"); } } }