死代码java eclipse

 mobiledu2502862777 发布于 2023-02-08 08:54

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

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

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

1 个回答
  • scanner = new Scanner(file);
    

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

    看到构造函数:

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

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

    2023-02-08 09:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有