2013-06-11 44 views
1

我有一個問題得到一個方法來讀取文件,然後將其轉換爲整數。以下是該計劃的簡要說明。它本質上是一個汽車經銷商庫存,通過將它們記錄在文本文件中來跟蹤車輛中的車輛。當程序啓動時,它需要讀取文件並將所有當前的汽車放入一個數組中,以便顯示它們。然後,程序的其餘部分將做其他事情,如刪除汽車和添加新聞等。我所在的部分是當程序首次啓動它需要閱讀文件,但我似乎無法得到它的工作。Java:問題讀取文本文件,然後轉換

該文本文件總共由6行組成; 4個數字分別爲2個字。我想要的方法來讀取前四行,並將其轉換爲整數並將它們存儲在一個臨時數組中。之後,它將讀取下兩行並將它們存儲在臨時數組中。之後,我將所有這些存儲的值發送給構造函數。然後將構造函數存儲在Arraylist中,並且可以隨時訪問Arraylist。在輸出中它確實很好。但是,儘管有障礙阻止這一點,但它希望第二次通過該方法。

這是代碼。它是一門課,而不是主要課程。我會盡量在代碼中儘可能地解釋程序。

public class Vehicle { 

    //All the different private variables for the constructors and methods 
    private int intholder[], year, type, kilometres, price, loop; 
    private String make, model, myline, holder[]; 

    //The Arraylist that the different vehicle objects will be stored 
    ArrayList<Vehicle> allCars = new ArrayList<Vehicle>(); 

    //The Default constructor 
    public Vehicle(){ 
     make = "Vehicle Make"; 
     model = "Vehicle Model"; 
     type = 0; 
     year = 0; 
     kilometres = 0; 
     price = 0; 
    } 

    //The constructor that has information sent to it 
    public Vehicle(int _type, int _year, int _kilometres, int _price, String _make, String _model){ 
     make = _make; 
     model = _model; 
     type = _type; 
     year = _year; 
     kilometres = _kilometres; 
     price = _price; 
    } 
    //Text file information 
    /* 
    * CAR TYPE CODE: 
    * 1 - Sedan 
    * 2 - Truck 
    * 3 - Crossover 
    * 4 - SUV 
    * 5 - Sports 
    * 
    * There is a total of 6 lines for each car and are as follows 
    * 1 - int Type integer 
    * 2 - int Year 
    * 3 - int Kilometres 
    * 4 - int Asking price 
    * 5 - String Make 
    * 6 - String Model 
    */ 

    //The method in question. It reads through the file, converts the integers and stores them, 
    //stores the strings, and sends all the information to the constructor 
    public void readCars()throws IOException{ 
     BufferedReader readFile = new BufferedReader(new FileReader("C:/Users/David/Desktop/FinalProject/Carlot.txt")); 

     //Setting the length of the temporary arrays 
     holder = new String[2]; 
     intholder = new int[4]; 

     //The main loop in the method. 
     do{ 

      //Read the first 4 lines of the file and convert them to integers. 
      //The try catch shouldn't have to be there because the first 4 lines 
      //of the file are all numbers, but I put it in there to see when it was messing up. 
      for(int i = 0; i < 4; i++){ 
       myline = readFile.readLine(); 
       try{ 
        intholder[i] = Integer.parseInt(myline); 
       } 
       catch(NumberFormatException e){ 
        System.out.println(e); 
       } 

       //Had this in here to see how many lines down the file it would go before messing up. 
       System.out.println(myline); 
      } 

      //Loop to store the Strings 
      for(int i = 0; i < 2; i++){ 
       myline = readFile.readLine(); 
       holder[i] = myline; 
       System.out.println(myline); 
      } 

      //Sends all the data to the constructor 
      Vehicle V = new Vehicle(intholder[0], intholder[1], intholder[2], intholder[3], holder[0], holder[1]); 

      //Several if statements to determine which subclass of vehicle it is. 
      if(intholder[0]==1){ 
       Sedan S = new Sedan(); 
       allCars.add(S); 
      } 
      else if(intholder[0]==2){ 
       Truck T = new Truck(); 
       allCars.add(T); 
      } 
      else if(intholder[0]==3){ 
       Crossover C = new Crossover(); 
       allCars.add(C); 
      } 
      else if(intholder[0]==4){ 
       SUV U = new SUV(); 
       allCars.add(U); 
      } 
      else if(intholder[0]==5){ 
       Sports P = new Sports(); 
       allCars.add(P); 
      } 

     //Only break the loop if the myline equals null 
     }while(myline != null); 

     //if the loop breaks, close the file  
     readFile.close(); 
    } 

現在我想我知道它出錯了。在do/while結束時,它檢查「myline」是否爲空。由於上次讀取文件,它仍然是一個字符串循環繼續。上次它經過循環時,一切都是空的,所以試圖轉換整數是不可能的,所以我得到錯誤。但我不知道如何讓它在循環結束時讀取文件,而不必去下一行。這是文本文件的樣子。

1 
2007 
150250 
5000 
Toyota 
Corolla 
2 
2005 
240400 
4500 
Chevorlet 
Silverado 

我不能擁有它在循環結束閱讀,因爲如果這樣做,並還有更多車一個我只是做了之後,它會進入下一行,當循環重新開始一切都拋關閉。

任何幫助表示讚賞,謝謝!

+1

你的問題會更簡潔,更容易回答,如果你只是有一個最小的,編譯能類告訴我們你的預期與實際產出。 –

+0

我現在所期望的所有輸出都是文本文件中的內容。現在,我得到了文本文件中的內容以及另外6行的null。 – Dave555

+0

你確定文件底部沒有空行嗎?您是否考慮過爲此添加檢查以及爲空?或者應該允許空行? – efan

回答

1

使用標籤break語句進行for循環簡單地將退出主do while循環時myline成爲null的。其他對象在循環中被實例化的方式並沒有留下很多空間容易重構,因此在這裏使用帶標籤的中斷是有意義的。

outerloop: 
do { 

    for (int i = 0; i < 4; i++) { 
    if ((myline = readFile.readLine()) == null) break outerloop; 
    // .. 
    } 

    for (int i = 0; i < 2; i++) { 
    if ((myline = readFile.readLine()) == null) break outerloop; 
    // .. 
    } 

    // .. 
} while (myline != null); 
+0

我試過了,它工作!謝謝!雖然我有一個關於外圈的問題。當你使用它時,它只適用於它下面的循環嗎?再次感謝。 – Dave555

+1

是的,就像給一個標籤(標籤可以是任何文本,如主循環等)到它下面的循環。但是,我建議您將與單個車輛有關的所有數據保存在文件中的同一行(以逗號分隔的值)。然後你可以通過檢查while(reader.readLine()!= null)'和簡單的'line.split(「,」)'每行來處理這些值來檢查EOF(文件結束)。 –

+0

好吧很高興知道,感謝您的幫助! – Dave555

0

也許你可以使用一個while循環,而不是一個do - while環和別的之前從文件中讀取下一行。事情是這樣的:

String myline = null; 
while((myline = readFile.readLine()) != null) { 

    // All your logic... 

} 

readFile.close(); 

while循環做以下條件:第一,讀取文件的下一行與myline = readFile.readLine()。前面的語句返回myline值,所以現在我們檢查它是不是null與比較:

(myline = readFile.readLine()) != null