title: Why is my program giving me the wrong output when I run it?
body: <p>I'm making a program where I look through a text file of 11 names, check to see if a certain name is in the file, add new names, delete names, and then close the file to update it.</p>
  
  <p>This is the data in directory.txt: (each name is on a new line)</p>
  
  <blockquote>
    <p>Mike<br>
     Jim<br>
     Barry<br>
     Cristian<br>
     Vincent<br>
     Chengjun<br>
     susan<br>
     ng<br>
     serena</p>
  </blockquote>
  
  <p>This is my helper class directory with all the possible things we can do to the names.</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  public class Directory {
     //public static void main(String[] args) {
     final int maxDirectorySize = 1024;
     String directory[] = new String[maxDirectorySize];
     int directorySize = 0;
     File directoryFile = null;
     Scanner directoryDataIn = null;
  
     public Directory(String directoryFileName) {
        directoryFile = new File(directoryFileName);
        try {
           directoryDataIn = new Scanner(directoryFile);
        }
        catch (FileNotFoundException e) {
           System.out.println("File is not found, exiting!" + directoryFileName);
           System.exit(0);
        }
        while (directoryDataIn.hasNext()) {
           directory[directorySize++] = directoryDataIn.nextLine();
        }
     }
     public boolean inDirectory(String name) {
        boolean inDir = true;
        for (int i = 0; i &lt; directory.length; i++) {
           if (name.equalsIgnoreCase(directory[i])) {
              inDir = true;
              break;
           }
           else {
              inDir = false;
              break;
           }
        }
        return inDir;
     }
     public boolean add(String name) {
     if (directory.length == 1024)
        return false;
        boolean added = true;
        for (int i = 0; i &lt; directory.length; i++) {
           if (directory[i].equalsIgnoreCase(name)) {
              added = false;
              break;
           }
           else {
              directory[directorySize++] = name;
              added = true;
              break;
           }
        }
        return added;
     }          
  
     public boolean delete(String name) {
        for (int i = 0; i &lt; directory.length; i++) {
           if (directory[i].equalsIgnoreCase(name)) {
              directory[i] = null;
              return true;
           }   
           else
              return false;
        }
        return false;
     }
  
     public void closeDirectory() {
        directoryDataIn.close();
        PrintStream directoryDataOut = null;
        try {
            directoryDataOut = new PrintStream(directoryFile);
        }
        catch (FileNotFoundException e) {
           System.out.printf("File %s not found, exiting!", directoryFile);
           System.exit(0);
        }
        String originalDirectory[] = {"Mike","Jim","Barry","Cristian","Vincent","Chengjun","susan","ng","serena"};
        if (originalDirectory == directory)
           System.exit(0);
        else
           for (int i = 0; i &lt; directorySize; i++)
              directoryDataOut.println(directory[i]);
           directoryDataOut.close();
     }
  }
  </code></pre>
  
  <p>This is my user interface class, the one i am running. Class DirectoryWithObjectDesign</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  public class DirectoryWithObjectDesign {
     public static void main(String[] args) { //throws IOException 
     String directoryDataFile  = "Directory.txt";
     Directory d = new Directory(directoryDataFile);
     Scanner stdin = new Scanner(System.in);
     System.out.println("Directory Server is Ready!");
     System.out.println("Format: command name");
     System.out.println("Enter ^Z to end");
     while (stdin.hasNext()) {
        String command = stdin.next();
        String name = stdin.next();
        if (command.equalsIgnoreCase("find")) {
           if (d.inDirectory(name))
              System.out.println(name + " is in the directory");
           else 
              System.out.println(name + " is NOT in the directory");
        }
        else if (command.equalsIgnoreCase("add")) {
           if (d.add(name))
              System.out.println(name + " added");
           else 
              System.out.println(name + " cannot add! " + "no more space or already in directory");
        }
        else if (command.equalsIgnoreCase("delete")) {
           if (d.delete(name))
              System.out.println(name + " deleted");
           else
              System.out.println(name + " NOT in directory");
        }
        else {
           System.out.println("bad command, try again");
        }
     }
     }
  }   
  </code></pre>
  
  <p>ANd finally, here is the faulty output I am getting:</p>
  
  <pre><code> ----jGRASP exec: java DirectoryWithObjectDesign
  
  Directory Server is Ready!
  Format: command name
  Enter ^Z to end
  find mike
  mike is in the directory
  find serena
  serena is NOT in the directory
  find susan
  susan is NOT in the directory
  find barry
  barry is NOT in the directory
  add melissa
  melissa added
  add joey
  joey added
  delete joey
  joey NOT in directory
  delete ng
  ng NOT in directory
  delete serena
  serena NOT in directory
  &lt;eof&gt;
  
   ----jGRASP: operation complete.
  </code></pre>
  
  <p>I was trying to figure out how I can fix it and tried some different ways but I'm still getting the wrong output. I don't see where the problem is or how to fix it..can anyone help?</p>