tags:

views:

108

answers:

5

I need to delete certain lines from a file, the file is a list of contacts that I read in from a file into my GUI. When I get to the contact I want to delete, my program should delete the contact from the file. I've tried to do this, but it is not working.

Here is the code I'm currently using:

String temp=txtname.getText();
for (Contact Contact:contacts)
{
    if (temp.equals(Contact.getname()));
    {
        txtname.setText("");
        txtsurname.setText("");
        txtphone.setText("");
        txtmobile.setText("");
        txtaddress.setText("");
        txtpostcode.setText("");
        contacts.remove(Contact);
        contacts.remove(Contact);
        contacts.remove(Contact);
        contacts.remove(Contact);
        contacts.remove(Contact);
        contacts.remove(Contact);
    }
}

My contact class is:

public class Contact {

    static void add(String text) {
    }
public String name;
public String surname;
public String phone;
public String mobile;
public String address;
public String postcode;

public Contact(){}

public Contact(String name, String surname, String phone,
                   String mobile, String address, String postcode)
    {
 this.name = name;
 this.surname = surname;
 this.phone = phone;
 this.mobile = mobile;
 this.address = address; 
 this.postcode = postcode; 
}

    public String getname()
    {
        return this.name;
    }
    public String getsurname()
    {
        return this.surname;
    }
    public String getphone()
    {
        return this.phone;
    }
    public String getmobile()
    {
        return this.mobile;
    }
    public String getaddress()
    {
        return this.address;
    }
    public String getpostcode()
    {
        return this.postcode;
    }

    public void setname(String name)
    {
        this.name = name;
    }
    public void setsurname(String surname)
    {
        this.surname = surname;
    }
    public void setphone(String phone)
    {
         this.phone = phone;
    }
    public void setmobile(String mobile)
    {
         this.mobile = mobile;
    }
    public void setaddress(String address)
    {
         this.address = address;
    }
     public void setpostcode(String postcode)
    {
         this.postcode = postcode;
    }
}

I'm guessing it deletes it from the arraylist, but I'm not sure how the program knows what to delete from the file.

Thanks.

+1  A: 

Modifying the internal list doesn't change the file. There is no automatic way to synchronize the two. You have to save the array back to the file to update it.

Aaron Digulla
+1  A: 

There is no way to delete anything from the middle of the file. The only way is to rewrite the file every time something should be changed.

Moisei
how would I go about doing this?, i'm already using the file in append mode to write to the file
addiosamigo
Well you could get fancy with java.io.RandomAccessFile, and append records to the end and "delete" Contacts in the middle by writing spaces to the first and last names or something. But in a homework assignment, just rewrite the file on any change (unless the point is to learn random file operations).
Jim Ferrans
the assignment is just to make a address book gui, that displays, adds, deletes and saves, oh and searchs (which is way to advanced for me to even contemplate!
addiosamigo
Assuming your contacts list is not big and performance is not an issue for you, implement a method that flushes list of contacts to the fileand on ANY modification in contacts of in contacts list delete the file and call this method.
Moisei
how would i go about doing this?
addiosamigo
+1  A: 

you can use a random access file but it seems like an over kill for this task. the best way to do it is to have the remove function write the whole file back to the disk.

Alon
how would I do this?
addiosamigo
A: 

Files can be rewrited, or appended to. In you case you'll have to rewrite it. There are other ways, but they would be an overkill here.

                String temp = txtname.getText();
                for (Contact contact : contacts) {
                    if (temp.equals(contact.getname())) {
                        contacts.remove(contact);
                        break;
                    }
                }

Fixed a lot of general problems with your code

public class Contact {
    private String name;
    private String surname;
    private String phone;
    private String mobile;
    private String address;
    private String postcode;

public Contact(String name, String surname, String phone, String mobile, String address, String postcode) {
    this.name = name;
    this.surname = surname;
    this.phone = phone;
    this.mobile = mobile;
    this.address = address; 
    this.postcode = postcode;       
}

public String getName() {
    return this.name;
}
public String getSurname() {
    return this.surname;
}
public String getPhone() {
    return this.phone;
}
public String getMobile() {
    return this.mobile;
}
public String getAddress() {
    return this.address;
}
public String getPostcode() {
    return this.postcode;
}

}

glebm
Careful, we don't want to do @adiosamigo's homework for him! ;-)
Jim Ferrans
what have you done? its the same?
addiosamigo
@jim ferrans, trust me i've done a LOT of work on this assignment!
addiosamigo
A: 

If you have no specific format for the file, then I suggest you to use the default serialization and serialize the contacts list. Like this,

//To serialize    
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("D:/newfile.txt")));
out.writeObject(contacts);

//To deserialize
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("d:/newFile.txt")));
contacts = (ArrayList<Contact>)in.readObject();

You first have to implement the Serializable interface in your Contact class.

Or if you need more control over the file format use the standard XML classes or JSON libraries.

sarav
that looks cool, but what does that do??
addiosamigo
by this you dont have to write a separate function to store/restore the contacts from your List to the File.
sarav