Hi all, can someone help me find what's going wrong in my code? When the program runs and it goes to get the user input, the program does not respond with any feedback, ie I type find Susan, and it does not tell me if Susan is in my txt file or not

Directory.Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Directory {
    //String array directory for holding directory items
    final int maxDirectorySize = 1024;
    String directory[] = new String[maxDirectorySize];
    int directorySize = 0;
    File directoryFile = null;
    Scanner directoryDataIn = null;
    Directory(String directoryFileName) {
        directoryFile = new File(directoryFileName);
        try {
            directoryDataIn = new Scanner(directoryFile);
        }
        catch (FileNotFoundException e) {
            System.out.printf("File %s not found, exiting!",
                    directoryFile);
            new File("Directory.txt");
            System.exit(0);
            //set up directoryFile to read from directoryFileName for reading
            //Load data from the file into the array directory
        }
        // Loading Directory
        while (directoryDataIn.hasNext())
            directory[directorySize++] = directoryDataIn.nextLine();

    }

    public boolean inDirectory (String name) {



        for(int i = 0; i<directorySize;)

            if (directory[i].equalsIgnoreCase(name)){
                return true;
            }


        return false;

    }

    public boolean add(String name) {

        if (directorySize == 1024)
            return false;
        directory[directorySize] = name;
        directorySize++;

        return true;
        //directory size will be increased by an amount of 1

    }
    //returns true if successful in adding name to directory; false otherwise

    public boolean delete(String name) {

        for (int i=0; i < directorySize; i++) {
            if (directory[i].equalsIgnoreCase(name)){
                for(int e = i;e   < directorySize-1; e++){
                    directory[e]=directory[e+1];
                }
                directorySize-=1;
                return true;
            }

        }
        return false;
    }





    public void closeDirectory() {
        directoryDataIn.close();
        // close explicitly before writing
        PrintStream directoryDataOut = null;
        // now open the directory data file for writing
        try {
            directoryDataOut = new PrintStream(directoryFile);
        }
        catch (FileNotFoundException e) {
            System.out.println("File %s not found, exiting!");
            System.exit(0);
        }
        // write updated directory back to file
        for (int i = 0; i < directorySize; i++)
            directoryDataOut.println(directory[i]);
        directoryDataOut.close();
    }

    //sets up the directoryFile for writing (after closing it)
    //write updated data to the directoryFile and close it
}

DirectoryWithObjectDesign.Java

import java.util.Scanner;

public class DirectoryWithObjectDesign {



    public static void main(String[] args) {

        String directoryDataFile = "Directory.txt";
        Directory d = new Directory(directoryDataFile);
        Scanner stdin = new Scanner(System.in);
        System.out.println("Please Enter One of The Following, Find, Add, Delete:");



        //Create/initialize the directory object
        //Tell the user the system is ready and waiting to execute commands

        while (stdin.hasNext()) {
            String command = stdin.next().trim();
            // using trim to get rid of leading whitespace
            String name = stdin.nextLine().trim();
            // using trim to get rid of leading + trailing whitespace
            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 + " 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");
                }
            stdin.close();
            d.closeDirectory();
        }
    }}

I think something is going wrong with my scanner possibly? i'm having trouble finding it

Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong, eg at line 23 print command and name, when adding entries to the directry print them, so you know for certain what's in the directory, etc.

You're probably having problems with the way the Scanner class works. Some times it returns an empty line when you use nextLine() after using next().
Printing out the values of the data read from the user will show if you are reading data as you want. Be sure to add an ID String and a delimiter to the print statements to show varialbe values:
System.out.println("varNameHere:"+ theVariableHere + "<");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.