面向对象的目录没有回报?

时间:2014-11-10 00:38:24

标签: java directory

我正在进行一项使用面向对象设计进行目录查找/数据库的任务。在输入命令和名称后,我的代码无法返回任何内容。如果有人能向我解释为什么会发生这种情况,我们将不胜感激!

2 个答案:

答案 0 :(得分:0)

永远不要关闭您的标准输入/输出/错误流;)

arg ....用平板电脑发臭。

在Directory#inDirectory方法中,你的for循环从不递增,所以永远不会完成。

答案 1 :(得分:0)

我运行了你的代码,这就是我找到的。

抛出" java.lang.IllegalStateException"在DirectoryWithObjectDesign类中,因为您正在尝试访问已关闭的扫描程序。

在你的" d.closeDirectory();"之后添加一个休息时间。命令和你的代码应该可以正常工作。

希望这有帮助!

import java.util.Scanner;

public class DirectoryWithObjectDesign {     public static void main(String [] args){

    String directoryDataFile = "D:/stackoverflow/StackJava/src/Directory.txt";
    Directory d = new Directory(directoryDataFile);
    Scanner stdin = new Scanner(System.in);
    System.out.println("Directory Server is Ready!");
    System.out.println("Available commands: find add delete");
    System.out.println("Format: command <name>\n" + "Enter ^Z to end\n");

    // 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();

        String name = stdin.next();
        System.out.println("command:" + command);
        System.out.println("name:" + name);

        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 + "has been added");
            else
                System.out.println(name + "is already in directory");
        } else if (command.equalsIgnoreCase("delete")) {
            if (d.delete(name))
                System.out.println(name + "has been deleted");
            else
                System.out.println(name + "is NOT in the directory");
        } else {
            System.out.println("Please try again");
        }
        stdin.close();
        d.closeDirectory();
        break;
    }
}

}