Hybrid Developer
Hybrid Developer

Reputation: 2340

How to remove a particular entry from a csv file?

I've a program in java to save and delete addresses into a csv file. Now i want to delete a particular address from the csv file, so that i wrote a function for that like

public String deleteAddress(String firstName,String LastName,String msg) throws IOException{
  Iterator<Address> it = addressBook.iterator();
  n=msg;
        while(it.hasNext()){
            Address newObj = it.next();
            if((newObj.getFirstName().equalsIgnoreCase(firstName) == true) && (newObj.getLastName().equalsIgnoreCase(LastName) == true)){
                File newfile = new File("address.csv");
                BufferedWriter bw = new BufferedWriter(new FileWriter(newfile));
                it.remove();
                n="found and removed";
            }
       }


       for(Address newObj : addressBook){
            AddressBookSave.saveAddressBookToFile(newObj);
      }
       return n;
}
}

but it is not working properly. Someone please help me to fix this.

Upvotes: 0

Views: 335

Answers (2)

pczeus
pczeus

Reputation: 7868

your code to perform the delete and add, including logic for loading and saving a file is missing or incomplete. It seems that what your are trying to do is actually pretty easy.

Not knowing exactly what your requirements enter code hereare, I can't hit it right on the mark. However, here is a complete sample class, which I wrote in Groovy (very close to Java - but better) that accomplishes what I 'think' you are trying to accomplish. If this does help you, please mark this example as a clear answer.

Feel free to run the below Groovy class and try it out.

import groovy.transform.Canonical

import javax.swing.*

public class AddressBook {
    List<Address> addressBook = new ArrayList<>();
    String filePath;

    public String deleteAddress(String firstName, String lastName) {
        String result = "Not Found"
        Iterator<Address> iter = addressBook.iterator();
        while(iter.hasNext()){
            Address addy = iter.next();
            if ((addy.getFirstName().equalsIgnoreCase(firstName)) && (addy.getLastName().equalsIgnoreCase(lastName))) {
                iter.remove();
                result = "Address was found and removed..save to update the file.";
            }
        }

        println result;
        return result;
    }

    void addAddress(String firstName, String lastName, String addyString){
        addressBook.add(new Address("firstName":firstName, "lastName":lastName, "address":addyString));
        println "Address added..save to update the file.";
    }

    void saveAddressBookToFile() throws IOException{
        BufferedWriter bw = null;
        println "Saving addresses to file: " + filePath;
        try{
            File file = new File(filePath);
            bw = new BufferedWriter(new FileWriter(file));
            file.createNewFile()
            for(Address addy: addressBook){
                bw.write(addy.toString() + "\n");
            }
            bw.flush();
        }
        finally{
            if(bw){
                bw.close();
            }
        }
    }

    void loadAddressBook(String path) throws IOException{
        addressBook.clear();
        filePath = path;
        BufferedReader br = null;
        try{
            List<String> lines = new BufferedReader(new FileReader(path)).readLines();
            for(String line: lines){
                addressBook.add(new Address(line));
            }
            println path + " loaded successfully."
        }
        catch(FileNotFoundException fnfe){
            println "File " + path + " does not exist, nothing loaded.";
        }
        finally {
            if (br) {
                br.close()
            }
        }
    }

    void printLoadedAddresses(){
        for(Address addy: addressBook){
            println addy.prettyString();
        }
    }

    public static void main(String... args) {
        AddressBook ab = new AddressBook();
        String readln = ' '

        while(readln){
            readln = JOptionPane.showInputDialog('Please input a valid command: load, add, delete, save')

            if (readln) {
                args = readln.split()
                switch (args[0]) {
                    case "load":
                        if (args.length < 2) {
                            println("You did not provide the second argument for the address csv file path.")
                            break;
                        } else {
                            ab.loadAddressBook(args[1])
                        }
                        break;
                    case "add":
                        if(args.length < 4){
                            println("Please provide the firstName, lastName, and address parameters when adding.")
                        }
                        else {
                            ab.addAddress(args[1], args[2], args[3..-1].join(" "));
                        }
                        break;
                    case "delete":
                        if(args.length < 3){
                            println("Please provide the firstName, lastName, and address parameters when deleting.")
                        }
                        else {
                            ab.deleteAddress(args[1], args[2]);
                        }
                        break;
                    case "save":
                        if (ab.filePath) {
                            ab.saveAddressBookToFile();
                        } else {
                            println("You must first call load with a file path for the csv file (even it it doesn't exist yet.")
                        }
                        break;
                    case "print":
                        ab.printLoadedAddresses();
                        break;
                    case "exit":
                        System.exit(0)
                    default:
                        println "Unrecognized command: " + args[0]
                }
            }
        }
    }
}

@Canonical
class Address {
    String firstName;
    String lastName;
    String address;

    public Address(){
        super
    }

    public Address(String csvString){
        List<String> tokens = csvString.split(",");
        firstName = tokens[0];
        lastName = tokens[1];
        address = tokens[2]
    }

    @Override
    public String toString(){
        return firstName + "," + lastName + "," + address;
    }

    public String prettyString(){
        return firstName + " " + lastName + " - " + address;
    }
}

Upvotes: 0

Shrikant Havale
Shrikant Havale

Reputation: 1290

Please refer to this link of SO, Boolean.TRUE == myBoolean vs. Boolean.TRUE.equals(myBoolean)

Here it is assumed that addressBook is properly populated with data. In your source code, you should not check it with == true as equalsIgnoreCase() method already will return either true or false and nothing else.

Change it to

if((newObj.getFirstName().equalsIgnoreCase(firstName)) && (newObj.getLastName().equalsIgnoreCase(LastName)))

Also to avoid NullPointerException, it is better to always put the value that you know already at first position, like this,

if((firstName.equalsIgnoreCase(newObj.getFirstName())) && (LastName.equalsIgnoreCase(newObj.getLastName())))

Upvotes: 1

Related Questions