2015-11-08 45 views
3

在編程課程中,我們正在學習I/O流。不幸的是,我的老師決定不去過去的兩個實驗室,並且已經取消了最後一次講座,離開了我的班級和我自己學習I/O流的基礎知識,以供我們最近的作業。從.txt文件讀取輸入存在嚴重問題

對於我們的最新作業,我們將從文本文件「ListOfAirlines.txt」中讀取信息,並將它們存儲在三個不同類(位置,指示符和航空公司)中。航空公司班級要有位置和指示員班的對象。

問題是我的程序好像不想讀取文件中的輸入。這是我迄今爲止的程序代碼。它沒有完成,但我試着測試我到目前爲止只是爲了看看它是否有效。每次運行它時,儘管我輸入了確切的文件名,catch塊仍是唯一運行的部分。我也得到一個運行時錯誤,說39行(for循環開始)有一個問題。我真的不知道自己在做什麼,而且我一直試圖在我的書中爲這個特定的程序做例子。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class AirlineOutput { 
    public static void storeInfo() { 
     AirlineInput object = new AirlineInput(); 
     String file = AirlineInput.enterFileName(); 
     Scanner inputStream = null; 

     try { 
      inputStream = new Scanner(new File(file)); 
     } catch (FileNotFoundException z) { 
      System.out.println("Error opening the file "+file); 
     } 

     /* 
     * The following creates an array for every object in the program. 
     * It cycles through the input stream assigning each line to a String variable. 
     * The String variables are then passed through to their respective object classes. 
     * The Location and Designator objects are passed to the airline object as the 
     * airline object is required to contain it's own Location and Designator objects. 
     */ 
     for (int i = 0; inputStream.hasNextLine(); i++) { 
      Airline[] airline = new Airline[i]; 
      airline[i]=new Airline(); 

      Designator[] designator = new Designator[i]; 
      designator[i]=new Designator(); 

      Location[] location = new Location[i]; 
      location[i]=new Location(); 

      String stringRead = inputStream.nextLine(); 
      Scanner parse = new Scanner(stringRead); 

      String airlineName = parse.next(); 
      String designators = parse.next(); 
      String frequentFlyerProgram = parse.next(); 
      String city = parse.next(); 
      String state = parse.next(); 
      String country = parse.next(); 
      String website = parse.next(); 

      location[i].setCity(city); 
      location[i].setCountry(country); 
      location[i].setState(state); 
      airline[i].setLocation(location[i]); 

      designator[i].setDesignators(designators); 
      airline[i].setDesignators(designator[i]); 

      airline[i].setName(airlineName); 
      airline[i].setWebsite(website); 
      airline[i].setFrequentFlyer(frequentFlyerProgram);} 
      inputStream.close(); 
     } 
    } 
} 

.enterFileName()位於我們需要的輸入類下面。這只是要求用戶輸入存儲在字符串中的文件名。

import java.util.Scanner; 

public class AirlineInput { 
    static String file; 

    public AirlineInput() { 

    } 

    public static String enterFileName() { 
     System.out.println("Enter the file name: "); 
     Scanner keyboard=new Scanner(System.in); 
     file=keyboard.next(); 

     return file;  
    } 
} 
+0

'enterFileName()'在哪裏?你能分享一下嗎? – Alexander

+2

有時文件位置可能會顯得有點奇怪......試試這個來找出程序在哪裏查找文件System.out.println(新文件(文件))。getAbsolutePath())'......在循環中出現錯誤的原因是因爲掃描器無法創建並且inputstream爲空。它不能在空值上調用hasNextLine。 – Numeron

+0

順便說一句:我會建議你正確縮進你的代碼。這可以使用大多數開發程序輕鬆完成。例如在eclipse中,它就是Shit + Alt + F。這使得代碼更易讀,更容易被大家理解。 – Frithjof

回答

3

的Java往往給人一種preety良好的反饋,這是不容易的第一眼,當你學習:)

引用你的問題的語言注意:

我每次運行它,catch塊是唯一運行的部分

哪給出反饋exc正在捕獲FileNotFoundException類的錯誤 - 錯誤發生,try-catch塊阻止應用程序崩潰。

documentation提取物說,這將引發異常:

  1. 當指定路徑名的文件不存在
  2. 如果文件不存在,也將被拋出,但由於某些原因不可訪問的時代碼

因此,在這種情況下,上述一個執行發生100%

異常後,被抓獲的程序進行,並符合代碼:

for(int i=0;inputStream.hasNextLine();i++) 

使程序崩潰,因爲變量inputStream沒有指定值,並呼籲未分配變量的方法可能提供了另一個異常。

我希望我的回答對java路徑有幫助和一路順風:)