Sign in to follow this  
markg

reading and writing to a file using ArrayList()

Recommended Posts

markg    122
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.

Share this post


Link to post
Share on other sites
X Abstract X    109
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]

Share this post


Link to post
Share on other sites
markg    122
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




Share this post


Link to post
Share on other sites
klee1    166
Your problem is right here in the code.

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.

Share this post


Link to post
Share on other sites
remigius    1172

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.

Share this post


Link to post
Share on other sites
markg    122
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.

Share this post


Link to post
Share on other sites
markg    122
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

Share this post


Link to post
Share on other sites
klee1    166
As remigius said, this class does not have override the toString() method from Object. That is why you don't get meaningful results when you try to print the contact to the file.

//Given a Contact c;
System.out.println(c);

translates to

//Given a Contact c;
System.out.println(c.toString());

If you don't override the method, then it will use Object's toString method, which is what you are currently outputting.

The method you would have to write in Contact is toString()

String toString()


Otherwise you could print each part of Contact when you're writing it to the file:

PrintWriter writeFile = new PrintWriter(file);
for(Contact c : aSaveContacts)
{
writeFile.println(c.getName());
writeFile.println(c.getAddress());
//Continue here...
}

Share this post


Link to post
Share on other sites
markg    122
Yep,
Your right, I did put in a toString() and it works. I am able to write to a file and when I open the file up I see all the info in english.
The other problem that I am now having is getting it to read in that file. here's the code. for reading in a file.

/* code begin.

public static void retrieveFile() throws FileNotFoundException, ParseException {
// TODO Auto-generated method stub

ArrayList<Contact> list = new ArrayList<Contact>();
list.clear();
Scanner input = new Scanner(System.in);

System.out.println("Enter the name of the file you would like to work with: ");

String fileName = input.nextLine();


Scanner read = new Scanner( new File(fileName));

while(read.hasNext()){

String line = read.next();
String[] lineData = line.split("|");
String date = lineData[0];
String name = lineData[1];
String phone = lineData[2];
String city = lineData[4];
String state = lineData[5];
String zip = lineData[6];

Contact contact = new Contact();
contact.setdateOfBirth(date);
contact.setName(name);
contact.setPhoneNumber(phone);
Address newAddress = new Address();
contact.setAddress(newAddress);
contact.setCity(city);
contact.setState(state);
contact.setZip(zip);

list.add(contact);
}

read.close();
}

*/ end code


Here are the Errors. I have no idea what they mean.

Exception in thread "main" java.lang.NullPointerException
at edu.itttech.labs.week06.Contact.toString(Contact.java:113)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at edu.itttech.labs.week06.AddressBookDemo2.retrieveFile(AddressBookDemo2.java:110)
at edu.itttech.labs.week06.AddressBookDemo2.main(AddressBookDemo2.java:52)

Share this post


Link to post
Share on other sites
mattd    1078
Quote:
Original post by markg
Here are the Errors. I have no idea what they mean.

Exception in thread "main" java.lang.NullPointerException
at edu.itttech.labs.week06.Contact.toString(Contact.java:113)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at edu.itttech.labs.week06.AddressBookDemo2.retrieveFile(AddressBookDemo2.java:110)
at edu.itttech.labs.week06.AddressBookDemo2.main(AddressBookDemo2.java:52)

That's actually indicative of just one error :). Your toString method in the Contact class is trying to call a method on a null reference. This is occuring on line 113 of Contact.java - inspect that line to see what's up.

Share this post


Link to post
Share on other sites
remigius    1172
Quote:
Here are the Errors. I have no idea what they mean.

Exception in thread "main" java.lang.NullPointerException


Cmon Mark, we're trying to help but put some effort into it. A NullPointerException is probably the singlemost frequent error you'll see from Java in your early programming days, so you should familiarize yourself with it. Also figuring out what a stack trace is should be one of your top priorities, these are invaluable to debugging.

As for the error itself I'll refer to mattd's reply above, but I'll wager your Address object in your Contact is probably null. The code you posted also doesn't match the error message, there is no System.out.println(Contact) anywhere in retrieveFile yet the error says there was. You're making it a challenge to help you [smile]

Share this post


Link to post
Share on other sites
markg    122
I know, I Know, I am going to have to put a little more effort into it. Now that you metioned about that stack trace I do rememger the instructor explaining it to us in class. I can be very forgetfull sometimes. It's all this learning that I am not used to. Any way I think I have to put this project on the back burner for now because I have been given another project that I have to start on for school. So I want to thank you for all the help that you have given me. I plan on picking up where I left off on this address book project probably at the end of this semester and I am sure I'll get it to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this