编译我的 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/