我有一個問題得到一個方法來讀取文件,然後將其轉換爲整數。以下是該計劃的簡要說明。它本質上是一個汽車經銷商庫存,通過將它們記錄在文本文件中來跟蹤車輛中的車輛。當程序啓動時,它需要讀取文件並將所有當前的汽車放入一個數組中,以便顯示它們。然後,程序的其餘部分將做其他事情,如刪除汽車和添加新聞等。我所在的部分是當程序首次啓動它需要閱讀文件,但我似乎無法得到它的工作。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
我不能擁有它在循環結束閱讀,因爲如果這樣做,並還有更多車一個我只是做了之後,它會進入下一行,當循環重新開始一切都拋關閉。
任何幫助表示讚賞,謝謝!
你的問題會更簡潔,更容易回答,如果你只是有一個最小的,編譯能類告訴我們你的預期與實際產出。 –
我現在所期望的所有輸出都是文本文件中的內容。現在,我得到了文本文件中的內容以及另外6行的null。 – Dave555
你確定文件底部沒有空行嗎?您是否考慮過爲此添加檢查以及爲空?或者應該允許空行? – efan