[Solved] Phonebook program doesn’t work in Java


This should fix both your issues..

I’ve made a number of changes to the code, so it will be difficult to explain everything, but I’ve included the main points after the code. Try going through it and add a comment in case of any doubt.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ContactListDriver {

    public static void main(String[] args) throws IOException, FileNotFoundException {
        File inputFile = new File("C:/Users/Gab Real/workspace/CSc121/MyContacts.txt");
        //System.out.println(String.format("File.canWrite() says %s", inputFile.canWrite()));

        //System.out.println(inputFile.getAbsolutePath());  
        BufferedReader reader = null;
        BufferedReader reader2 = null;
        BufferedReader reader3 = null;
        FileWriter out = null;
        String ls = System.getProperty("line.separator");
        //File tempFile = new File("temp1.txt");
        try {
            Scanner scan = new Scanner(System.in);
            String word = "";

            System.out.println("oooooooooooooooooooooo[Instructions]ooooooooooooooooooooooooooooooooooo");
            System.out.println("o use command 'show' to show all contacts                             o");
            System.out.println("o use command 'add' to add a contact (e.g. add Gabrielle 25485878555) o");
            System.out.println("o use command 'remove' + the name of the contact to remove a Contact  o");
            System.out.println("o use command 'exit' to exit the program                              o");
            System.out.println("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");

            while (!word.contentEquals("exit")) {
                System.out.println("\nEnter a command: ");
                word = scan.nextLine();
                String entries[] = word.split(" ");

                if (entries[0].equalsIgnoreCase("add")) {
                    out = new FileWriter(inputFile, true);
                    out.write(ls + entries[1] + " " + entries[2]);
                    out.flush();
                    //out.println(out.toString());
                    //out.write("\r\n");
                } else if (entries[0].equalsIgnoreCase("remove")) {
                    String allLines = "";
                    String currentLine;
                    reader = new BufferedReader(new FileReader(inputFile));

                    while ((currentLine = reader.readLine()) != null) {
                        allLines = allLines + "|" + currentLine;
                    }
                    String[] allLinesArr = allLines.split("\\|");

                    out = new FileWriter(inputFile, false);
                    for (int i = 0; i < allLinesArr.length; i++) {
                        currentLine = allLinesArr[i];
                        if (currentLine.contains(entries[1])) {
                            System.out.println("Deleting " + entries[1]);
                            continue;
                        }
                        if (i == 0) {
                            out.write(currentLine);
                        } else {
                            out.write(ls + currentLine);
                        }
                    }
                    out.flush();
                } else if (entries[0].equalsIgnoreCase("show")) {
                    reader = new BufferedReader(new FileReader(inputFile));
                    String currentLine;
                    while ((currentLine = reader.readLine()) != null) {
                        // trim newline when comparing with lineToRemove
                        System.out.println(currentLine);
                    }
                }
            }

        } finally {
            if (out != null) {
                out.close();
            }
            if (reader != null) {
                reader.close();
            }
        }

    }
}

Main points to note:

  • The 2nd parameter (true/false) in PrintWriter denotes whether to append (true) or overwrite (false)
  • BufferedReader is declared inside functions so that it restarts reading from file start (solving your show multiple times problem)
  • PrintWriter in ‘remove’ is mid-way because, on its declaration, the file will be emptied (hence I have first taken it’s contents into a ‘|’ delimited String)

Only the multiple-remove part is left. The logic to do that will be to allow the user to enter multiple names separated by ” ” (hence populating your entries array) & inside the remove bit, looping over it for the array length (from index = 1 to index = entries.length) and putting the if condition inside it.

Note: I’m not sure whether the if conditions I’ve put inside the finally block are necessary. But its safer to keep it there.

13

solved Phonebook program doesn’t work in Java