0

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.

Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55
  • 1
    What is `addressBook`? You call `iterator()` on it at the top of your method but there's no variable called `addressBook` defined until the `for` loop later in the method. Also, you're never writing to your `BufferedWriter`. – Joe Attardi Jan 18 '16 at 16:28
  • addressBook is a set – Hybrid Developer Jan 18 '16 at 16:40
  • It also looks like nothing is being done with the `File new File` logic. Also, is your intent to completely overwrite the contents of the file after removing/modifying the address entry? What is the contents of the saveAddressBookToFile method? – pczeus Jan 18 '16 at 17:15
  • If this works for you, please mark the post as answered. – pczeus Jan 20 '16 at 16:07

2 Answers2

1

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())))
Community
  • 1
  • 1
Shrikant Havale
  • 1,250
  • 1
  • 16
  • 36
  • What you are saying is absolutely correct Shrikant, but doesn't answer or solve the problem. Despite the 'sloppy' syntax, his if condition does work and evaluate the same as your modified one. – pczeus Jan 18 '16 at 17:14
0

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;
    }
}
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • Shall i call `saveAddressBookToFile()` in `deleteAddress(String firstName, String lastName)` after `result = "Address was found and removed..save to update the file.";` – Hybrid Developer Jan 20 '16 at 06:20
  • Yes, the you need to call save as a separate command/operation. – pczeus Jan 20 '16 at 16:06
  • You could modify the code to call the save method within the add and/or delete methods to have the save more 'automatic' after a change is made. – pczeus Jan 20 '16 at 16:06