1

我正在尝试检查用户给出的单词是否已经存在于文本文件中,或者它的子字符串是否已经存在。这是我的代码:

String ans = null;
Scanner scanner = null;
do
{
    System.out.print("Please enter a new word: ");
    String Nword = scan.next();
    System.out.print("And its appropriate Hint: ");
    String Nhint = scan.next();

    Word word = new Word(Nword , Nhint);
    File file = new File("C:\\Users\\Charbel\\Desktop\\Dictionary.txt");
    file.createNewFile();
    scanner = new Scanner(file);
    if (scanner != null)
    {
        String line;
        while (scanner.hasNext()) {
            line = scanner.next();
            for(int i = 0 ; i<line.length(); i++)
                if ((line.equals(Nword)) || (Nword.equals(line.substring(i))))
                {
                    System.out.println("The word already exists.");
                    break;
                }
        }
    }
    else
    {
        FileWriter writer = new FileWriter(file , true);
        writer.write(word.toString());
        writer.write(System.lineSeparator());
        writer.flush();
        writer.close();

        System.out.println("Your word has successfuly added.");
        System.out.print("\nWould you like to add another word ?\nPress 0 to continue.");
        ans = scan.next();
    }
} while(ans.equals("0"));

Eclipse 说else条件后面的语句是“死代码”,我不知道为什么。

4

3 回答 3

5
scanner = new Scanner(file);

scanner已初始化,永远不可能null,因此else永远不会到达该语句。

查看构造函数

抛出:
FileNotFoundException- 如果找不到源

因此,如果file不存在,scanner则不会null,您将遇到异常

于 2013-12-26T15:15:25.743 回答
2
scanner = new Scanner(file);

该语句在这里创建一个新实例。所以这个: if (scanner != null)永远不会是假的。

于 2013-12-26T15:19:00.100 回答
0

Dead-Codeis 永远不会被执行,例如:

if(true) {
// do something
}else {
// do something else <-- this is dead code, or else-block is dead code
}

在您的情况下,因为是在无法执行关联Scanner之前创建的。如果创建失败,将再次抛出错误,该错误将不会被执行,因此从编译器的角度来看,没有机会执行块。if(scanner != null)elseScannerelseelsedead-code

if-elsescanner如果将实例作为参数传递,那将是有意义的。

要解决这个问题,else应该删除!

以下应该更正您的代码:

                scanner = new Scanner(file);
                FileWriter writer = new FileWriter(file, true);
                if (scanner != null) {
                    String line;
                    while (scanner.hasNext()) {
                        line = scanner.next();
                        for (int i = 0; i < line.length(); i++)
                            if ((line.equals(""))
                                    || ("".equals(line.substring(i)))) {
                                System.out.println("The word already exists.");
                                break;
                            } else {
                                writer.write(word.toString());
                                writer.write(System.lineSeparator());
                                writer.flush();
                                System.out.println("Your word has successfuly added.");
                                System.out.print("\nWould you like to add another word ?\nPress 0 to continue.");
                                ans = scan.next();
                            }
                    }
                }
                writer.close();
于 2013-12-26T15:15:58.943 回答