2014-12-03 70 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)); 
}