java - 编译java程序filenotfound异常

标签 java exception compilation filenotfoundexception

编译我的 java 文件时遇到问题,我认为问题出在这里: 面临的问题是我必须包含 filenotfound 异常。但是,当我添加它时,编译器给我一个错误“重写的方法不会抛出 filenotfoundException”关于如何解决这个问题有任何想法吗?

public String getArrival(String flightNumber) {
   Scanner scanner = new Scanner(new File("flights.txt"));
   while(scanner.hasNextLine()){
      String s = scanner.nextLine();
      if(s.indexOf(flightNumber)!=-1){
         String city = s.split("-")[1];
         System.out.println("getArrival(): " + flightNumber + " ==>     Arrival city is " + city);
         return city;
      }
   } 
}

最佳答案

您必须使用 try/catch 自己处理 FileNotFoundException。

试试这个...

Scanner scanner = null;
    try {
        scanner = new Scanner(new File("flights.txt"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

关于java - 编译java程序filenotfound异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553991/

相关文章:

java eclipse mars libgdx 在我的桌面项目包上找不到 main 来运行游戏屏幕

java.lang.ClassCastException 套接字 readObject

amazon-web-services - Lambda AWS - 异常

exception - Junit 异常处理

java - 如何用Ant编译构建C++项目?

java - 通过 Gmail API Google App Engine 发送电子邮件

java - 服务中存储类的同步和/或互斥

java - 正在运行的处理器会发出重复类警告

linux - 使用 Eclipse 从 Portaudio 编译 paex_record_file.c 时未定义对 `PaUtil_GetRingBufferReadAvailable' 的引用

java - 如何在没有堆栈/正则表达式的情况下检查平衡括号?