死代码java eclipse

Cha*_*bel 1 java dead-code

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

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"));
Run Code Online (Sandbox Code Playgroud)

Eclipse说else条件之后的语句是"死代码",我不知道为什么.

Mar*_*oun 5

scanner = new Scanner(file);
Run Code Online (Sandbox Code Playgroud)

scanner被初始化,永远不会null,所以else永远不会达到这个陈述.

看到构造函数:

抛出:
FileNotFoundException- 如果找不到来源

因此,如果file不存在,scanner将不存在null,您将有例外.