Xiangcheng Ding 2023-05-08 19:20 采纳率: 62.5%
浏览 36
已结题

cannot be resolved to a variable 问题


public static ArrayList<String> readFile(File inputFile){
    try{
      Scanner in = new Scanner(inputFile);
      ArrayList<String> codes = new ArrayList<String>();
      while(in.hasNextLine()){
        codes.add(in.nextLine());
      }
      in.close();
    }catch(IOException exception){
      exception.printStackTrace();
    }catch(NoSuchElementException exception){
      System.out.println(exception.getMessage());
    }catch(Exception exception){
      System.out.println(exception.getMessage());
    }
    return codes;
  }

这段代码为何报错 codes cannot be resolved to a variable.(return那一行)

  • 写回答

1条回答 默认 最新

  • 创意程序员 2023-05-08 19:37
    关注

    codes的生命期只限于try里面,提到前面就行了:
    ArrayList<String> codes = null;
    try里面直接给codes赋值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 创建了问题 5月8日