reading and writing to a file using ArrayList()
I was wondering if anybody could help me. I have a project due tommarrow and the project was createing an address book that will read and write contact information to a file. I have all of it done except the reading and writing the information to a file. I tried some things, but no luck. the project requires that I use an ArrayList, which I have. I can input data I am able to store the data in the ArrayList. but having trouble saving it to a file. When I tried one method and opened up the file I got this: [edu.itttech.labs.week06.Contact@10b30a7] I am stuck and don't know what else to try. can anyone please help me out.
Post your code. And what language is this? Java?
I'm going to take a guess. You're trying to write the actual object to the file. You have to write the contents of the array list to the file.
[Edited by - X Abstract X on February 7, 2010 11:26:52 PM]
I'm going to take a guess. You're trying to write the actual object to the file. You have to write the contents of the array list to the file.
[Edited by - X Abstract X on February 7, 2010 11:26:52 PM]
Yes, it java. Here's the code that I have so far. I did have a little more but my idea didn't work so I erased it. Like I said I'm stuck and don't know where to go from here. I very new to programming. This first one is to get a file and the second one is to write to a file.
/* begin code.
public static void retrieveFile() throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
ArrayList<Contact> list = new ArrayList<Contact>();
System.out.println("Enter the name of the file you would like to work with: ");
String fileName = input.next();
Scanner read = new Scanner( new File(fileName));
while(read.hasNext())
}
*/ end code
/* begin code
public static void saveData(List<Contact> aSaveContacts) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("Enter a file name for this data to be saved to: ");
String fileName = input.next();
File file = new File(fileName);
PrintWriter writeFile = new PrintWriter(file);
for(int i = 0; i < aSaveContacts.size(); i++){
writeFile.println(aSaveContacts);
}
writeFile.close();
System.out.println("your data has been saved to file: " + fileName);
}
}
*/ end code
/* begin code.
public static void retrieveFile() throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
ArrayList<Contact> list = new ArrayList<Contact>();
System.out.println("Enter the name of the file you would like to work with: ");
String fileName = input.next();
Scanner read = new Scanner( new File(fileName));
while(read.hasNext())
}
*/ end code
/* begin code
public static void saveData(List<Contact> aSaveContacts) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("Enter a file name for this data to be saved to: ");
String fileName = input.next();
File file = new File(fileName);
PrintWriter writeFile = new PrintWriter(file);
for(int i = 0; i < aSaveContacts.size(); i++){
writeFile.println(aSaveContacts);
}
writeFile.close();
System.out.println("your data has been saved to file: " + fileName);
}
}
*/ end code
Your problem is right here in the code.
As X Abstract X noted, you are writing the ArrayList object to the file, as opposed to writing the Contact that's in the ArrayList.
I would suggest something like this
This code will print the Contacts in the aSaveContacts ArrayList to the file.
Using this enhanced for loop you don't have to deal with the temporary i variable, and it makes the code more readable.
PrintWriter writeFile = new PrintWriter(file);for(int i = 0; i < aSaveContacts.size(); i++){ writeFile.println(aSaveContacts);}
As X Abstract X noted, you are writing the ArrayList object to the file, as opposed to writing the Contact that's in the ArrayList.
I would suggest something like this
PrintWriter writeFile = new PrintWriter(file);for(Contact c : aSaveContacts) writeFile.println(c);
This code will print the Contacts in the aSaveContacts ArrayList to the file.
Using this enhanced for loop you don't have to deal with the temporary i variable, and it makes the code more readable.
Your edu.itttech.labs.week06.Contact class doesn't seem to have a ToString() method, so it prints out some default information. If this class is provided in the assignment, the assignment probably contains some details on how you're expected to deal with this.
Seeing you've been at it for a while I doubt your instructor can/will help you if it's due tomorrow, but either he or your tutor would be the person to talk to if the assignment is unclear.
Well, I know the instructor will help me to complete project and then allow me to hand it in for grading before the end of class, I was just hoping I could complete this on my own with out his help. But it looks like I am going to have to get his help on this one. Thank's anyway.
Here's the contact class
/*begin code
ackage edu.itttech.labs.week06;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Contact {
private String name = null;
private Date dateOfBirth = null;
private String phoneNumber = null;
private Address address = null;
public Contact(){}
public Contact(String aName, Date aDateOfBirth, String aPhoneNumber){
this.name = aName;
this.dateOfBirth = aDateOfBirth;
this.phoneNumber = aPhoneNumber;
}
public String getName() {
return name;
}
public void setName(String aName) {
this.name = aName;
}
public void setDateOfBirth(Date aBirthDate) {
this.dateOfBirth = aBirthDate;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String aPhoneNumber) {
this.phoneNumber = aPhoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address aAddress) {
this.address = aAddress;
}
protected String getContacts(){
return name + dateOfBirth + phoneNumber;
}
*/ end code
/*begin code
ackage edu.itttech.labs.week06;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Contact {
private String name = null;
private Date dateOfBirth = null;
private String phoneNumber = null;
private Address address = null;
public Contact(){}
public Contact(String aName, Date aDateOfBirth, String aPhoneNumber){
this.name = aName;
this.dateOfBirth = aDateOfBirth;
this.phoneNumber = aPhoneNumber;
}
public String getName() {
return name;
}
public void setName(String aName) {
this.name = aName;
}
public void setDateOfBirth(Date aBirthDate) {
this.dateOfBirth = aBirthDate;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String aPhoneNumber) {
this.phoneNumber = aPhoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address aAddress) {
this.address = aAddress;
}
protected String getContacts(){
return name + dateOfBirth + phoneNumber;
}
*/ end code
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement