title: Phonebook program doesn't work in Java
body: <p>I'm creating a program that acts like a Phonebook. I have my Contacts written in a text file. My program should read the contacts in the text file, and also also allow the user to add a contact and remove a contact. By doing this it should also update the text file.
  So here's my code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.PrintWriter;
  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;
      PrintWriter out = null;
      File tempFile = new File("temp1.txt");
      try{
      reader = new BufferedReader(new FileReader(inputFile));
      out = new PrintWriter(tempFile);
      Scanner scan = new Scanner(System.in);
      String word = "";
  
      System.out.println("oooooooooooooooooooooo[Instructions]ooooooooooooooooooooooooooooooooooo");
      System.out.println("o use command 'add' to add a contact (e.g. add Gabrielle 2548-5878-555) 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(word.startsWith("add")){
          out.append(entries[1] + " " + entries[2]);
          //out.println(out.toString());
          //out.write("\r\n");
      } else if (word.contains("remove")){
          String currentLine;
          while((currentLine = reader.readLine()) != null) {
              // trim newline when comparing with lineToRemove
              String trimmedLine = currentLine.trim();
              if(trimmedLine.contains(entries[1])) continue;
              out.println(currentLine);
          }
      } else if (word.contains("show")){
          String currentLine;
          while((currentLine = reader.readLine()) != null) {
              // trim newline when comparing with lineToRemove
              System.out.println(currentLine); 
          }
      }
  }
  
  
      }
      finally {
          out.close();
          reader.close();
          }
  
    }
  }
  </code></pre>
  
  <p>The problems that I encountered as as follows:</p>
  
  <ol>
  <li><p>Some times when I add a contact the text file gets empty after exiting the program.</p></li>
  <li><p>I can only show/remove a contact once. It doesn't allow me to show the contacts many times or delete multiple contacts.</p></li>
  </ol>