Dead code java eclipse



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

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条件之后的陈述是"死代码",我不知道为什么。

scanner = new Scanner(file);

scanner初始化,永远无法null,因此永远不会到达else语句。

请参阅构造函数:

投掷:
FileNotFoundException - 如果未找到源

因此,如果file不存在,scanner就不会null,您将有一个例外

scanner = new Scanner(file);

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

Dead-Code永远不会

被执行,例如:

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

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

如果scanner实例作为参数传递,if-else是有意义的。

为了解决这个问题,应该删除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();

最新更新