2014-12-03 71 views
2

我的代码读取一个文件,其中文件名由用户键入。如果用户键入一个不存在的文件名,那么它会捕获该异常并打印未找到的文件。我试图做的是让代码循环,如果文件名无效。但是,会发生什么情况是代码一直在打印未找到的文件,并且不会停止。那么我的代码有什么问题?如何让程序检查错误,然后继续循环

public static Scanner readFile(String filename){ 
    File input = new File(filename); 
    Scanner sc = null; 
    do { 
     try { 
      sc = new Scanner(input); 
     } 
     catch(FileNotFoundException e) { 
      System.out.println("Filename not valid"); 
     } 
    } while (!new File(filename).exists()); 
    return sc; 
} 

没有答案帮助我,所以也许我会尝试发布整个代码,看看是否有帮助。

import java.io.*; 
import java.util.*; 

public class Report{ 
    public static void main(String[] args){ 
     Scanner scanner = new Scanner(System.in); 
     String filename = scanner.next(); 
     Scanner input = readFile(filename); 
     CO2Data[] aDataArray = null; 
     aDataArray = readData(filename); 
     String highestvalue = highest(aDataArray); 
     String lowestvalue = lowest(aDataArray); 
     String highest_road = highest_per_person(aDataArray); 
     String lowest_road = lowest_per_person(aDataArray); 
     try{ 
     PrintStream output = new PrintStream(new File("Report.txt")); 
     output.println("The country with the lowest CO2 emissions is " + lowestvalue); 
     output.println("The country with the highest CO2 emissions is " + highestvalue); 
     output.println(); 
     output.println("The country with the lowest per person road emissions is " + lowest_road); 
     output.println("The country with the highest per person road emissions is " + highest_road); 
     } 
     catch(FileNotFoundException e){ 
      System.out.println("Error printing to file"); 
      System.exit(-1); 
      } 
     } 

    public static Scanner readFile(String filename){ 
    Scanner stdin = new Scanner(System.in); 
    File input; 
    do { 
     input = new File(filename); 
     try { 
      stdin = new Scanner(input); 
     } 
     catch(FileNotFoundException e) { 
      System.out.println("Filename not valid. Please try again:"); 
      filename = stdin.nextLine(); 
     } 
    } while (!input.exists()); 
    return stdin; 
} 

    public static CO2Data[] readData(String filename){ 
    File input = new File(filename); 
     Scanner sc = null; 
     try{ 
      sc = new Scanner(input); 
     } 
     catch(FileNotFoundException e){ 
      System.out.println("Filename not valid"); 
      System.exit(-1); 
     } 
    String info = sc.nextLine(); 
    int total = sc.nextInt(); 
    CO2Data[] arr = new CO2Data[total]; 
    for(int i=0; i<10;i++){ 
     arr[i] = new CO2Data(); 
     } 
    for(int i=0; i<10;i++){ 
     arr[i].setCountry(sc.next()); 
     arr[i].setTotalCO2(sc.nextDouble()); 
     arr[i].setRoadCO2(sc.nextDouble()); 
     arr[i].setCO2PerPerson(sc.nextDouble()); 
     arr[i].setCarsPerPerson(sc.nextInt()); 
     } 
    return arr; 
    } 

    public static String highest (CO2Data [] arr2){ 
    Scanner sc = new Scanner(System.in); 
    CO2Data highestindex = arr2[0]; 
    for (int i = 0; i<arr2.length; i++){ 
     if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){ 
      highestindex = arr2[i]; 
      } 
     } 
    return highestindex.getCountry(); 
    } 

    public static String lowest (CO2Data [] arr3){ 
     Scanner sc = new Scanner(System.in); 
     CO2Data lowestindex = arr3[0]; 
      for (int i = 0; i<arr3.length; i++){ 
       if (arr3[i].getTotalCO2() < lowestindex.getTotalCO2()){ 
        lowestindex = arr3[i]; 
        } 
       } 
     return lowestindex.getCountry(); 
     } 

      public static String highest_per_person (CO2Data [] arr2){ 
    Scanner sc = new Scanner(System.in); 
    CO2Data highestindex = arr2[0]; 
    for (int i = 0; i<arr2.length; i++){ 
     if (arr2[i].getRoadCO2() > highestindex.getRoadCO2()){ 
      highestindex = arr2[i]; 
      } 
     } 
    return highestindex.getCountry(); 
    } 

    public static String lowest_per_person (CO2Data [] arr3){ 
     Scanner sc = new Scanner(System.in); 
     CO2Data lowestindex = arr3[0]; 
      for (int i = 0; i<arr3.length; i++){ 
       if (arr3[i].getRoadCO2() < lowestindex.getRoadCO2()){ 
        lowestindex = arr3[i]; 
        } 
       } 
     return lowestindex.getCountry(); 
     } 
    } 
+0

你不是要求在循环中有更多的输入,并且你永远不会为传递给函数的参数指定'filename'。 – khelwood 2014-12-03 20:34:52

+0

你所有的循环正在做的是重复检查相同的文件名一遍又一遍。 – 2014-12-03 20:35:07

+0

我知道,但问题是我不知道如何使代码检查另一个文件。 – 2014-12-03 20:37:35

回答

0

当文件名是无效的,你应该得到的,而不是仅仅重新检查相同的文件名一遍一遍来自用户的新的文件名。例如: -

Scanner stdin = new Scanner(System.in); 
File input = new File(filename); 
Scanner sc = null; 
do { 
    try { 
     sc = new Scanner(input); 
    } 
    catch(FileNotFoundException e) { 
     System.out.println("Filename not valid. Please try again:"); 
     filename = stdin.readLine(); 
    } 
} while (!new File(filename).exists()); 
+0

我遇到了该方法的问题。如果我输入错误的文件名,然后输入正确的文件名,程序无法识别正确的文件名,然后在我的程序中的另一种方法中无法识别文件。该方法应该返回sc,所以当我使用你的代码返回sc时sc会发生什么情况? – 2014-12-03 20:59:21

0

readFile(String filename)收到filename参数,但如果这样的文件不存在,有什么可以做的方法?它当然不能自己创造一个正确的文件名。 所以这个问题在这个方法之外。您需要确保使用有效的文件名称调用此方法。

public Scanner getScanner() { 
    Scanner stdin = new Scanner(System.in); 
    while (true) { 
     String path = null; 
     try { 
      System.out.println("Please enter path to input file: "); 
      path = stdin.nextLine(); 
      return getScanner(path); 
     } catch (FileNotFoundException e) { 
      System.err.println("No such file: " + path); 
     } 
    } 
} 

private Scanner getScanner(String path) throws FileNotFoundException { 
    return new Scanner(new File(path)); 
}