Question: In Java, how do I get this code to write to text file and look identically to the output to console like this sample. ALL
In Java, how do I get this code to write to text file and look identically to the output to console like this sample. ALL i get is something looking like --> homework6.Businesscontact@3d4eac69
WHEN I NEED IT TO LOOK LIKE THE BELOW SAMPLING as a text file.
Contact first name: Ben
Contact last name: Allen
Contact email address: ballen@yahoo.com
Contact Phone Number: 918-489-4231
Contact City: Tulsa
Contact type: homework6.Friend
Date of birth: 12/05/79
Contact first name: Armand
Contact last name: Etame
Contact email address: aetame@aol.com
Contact Phone Number: 918-493-1001
Contact City: Tulsa
Contact type: homework6.Friend
Date of birth: 11/12/79
package homework6;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Maindriver {
public static void main(String[] args) {
String filename = "output.txt";
String line;
/*try{*/
ArrayList contacts = new ArrayList<>();
MyContact friend1 = new Friend("Ben", "Allen", "ballen@yahoo.com", "918-489-4231", "Tulsa", "12/05/79");
MyContact friend2 = new Friend("Armand", "Etame", "aetame@aol.com", "918-493-1001", "Tulsa", "11/12/79");
MyContact friend3 = new Friend("Adam", "Faurie", "afaurie@cox.net", "918-299-1218", "Tulsa", "12/26/79");
MyContact friend4 = new Friend("Kevin", "O'keefe", "kokeefe@)sbcglobal.net", "918-564-1284", "Tulsa", "12/31/81");
MyContact bcontact1 = new Businesscontact("Joe", "Gibbons", "jgibbons@techspecs.com", "918-435-4231", "Tulsa", "Tech Specs");
MyContact bcontact2 = new Businesscontact("Mary", "Reynolds", "mreynolds@redwoodtech", "918-742-2189", "Tulsa", "Redwood Tech Inc.");
MyContact bcontact3 = new Businesscontact("Clark", "Benson", "cbenson@softwarenerds.com", "918-490-4231", "Tulsa","Software Nerds");
MyContact bcontact4 = new Businesscontact("Ryan", "Simmons", "rsimmons@databaseexperts.com", "918-749-2312", "Tulsa", "Data Experts");
contacts.add(friend1);
contacts.add(friend2);
contacts.add(friend3);
contacts.add(friend4);
contacts.add(bcontact1);
contacts.add(bcontact2);
contacts.add(bcontact3);
contacts.add(bcontact4);
for (MyContact contact :contacts){
System.out.println(contact.display());
}
try {
FileWriter fw = new FileWriter (filename);
Writer output = new BufferedWriter(fw);
int sz = contacts.size();
for (int i=0; i < sz; i++){
output.write(contacts.get(i).toString() + " ");
}
output.close();
} catch (Exception e){
JOptionPane.showMessageDialog(null, "doesn't work");
}
}}
package homework6;
import java.io.Serializable;
public abstract class MyContact implements Myinterface {
private final String firstName;
private final String lastName;
private final String emailAddress;
private final String phoneNumber;
private final String city;
public MyContact(String firstName, String lastName, String emailAddress, String phoneNumber, String city) {
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
this.phoneNumber = phoneNumber;
this.city = city;
}
public String display(){
return " Contact first name: \t" + firstName + " Contact last name: \t" + lastName + " Contact email address: \t" +
emailAddress + " Contact Phone Number: \t" + phoneNumber + " Contact City: \t\t" + city + " Contact type: \t\t" + this.getClass().getName() + "" + " ";
/*public String toString()
{
return " First Name: " + getFirstName() + " Last Name:\t " + getLastName() + " Email Address:\t " +
getEmailAddress() + " Phone Number:\t " + getPhoneNumber() + " City:\t\t " + getCity();
}
public void contacts(){
System.out.println("Contacts");*/
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getCity() {
return city;
}}
package homework6;
import java.io.Serializable;
public class Friend extends MyContact implements Serializable, Myinterface {
private final String dateofBirth;
public Friend(String firstName, String lastName, String emailAddress, String phoneNumber, String city, String dateofBirth) {
super(firstName, lastName, emailAddress, phoneNumber, city);
this.dateofBirth = dateofBirth;
}
@Override
public String display(){
return super.display() + "Date of birth: \t\t" + dateofBirth;
}
public String getDateofBirth() {
return dateofBirth;
}
/*public void contacts(){
System.out.println( "These are my friends.");
}*/
}
package homework6;
public class Businesscontact extends MyContact implements Myinterface{
private String employer;
public Businesscontact(String firstName, String lastName, String emailAddress, String phoneNumber, String city, String employer) {
super(firstName, lastName, emailAddress, phoneNumber, city);
this.employer=employer;
}
@Override
public String display(){
return super.display() + "Employer:\t " + employer;
/*public void contacts(){
System.out.println( " These are my business contacts.");
}
public String getEmployer() {
return employer;
}
public void setEmployer(String employer) {
this.employer = employer;
}*/
}
public String getEmployer() {
return employer;
}
public void setEmployer(String employer) {
this.employer = employer;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
