我有一个问题得到一个方法来读取文件,然后将其转换为整数。以下是该计划的简要说明。它本质上是一个汽车经销商库存,通过将它们记录在文本文件中来跟踪车辆中的车辆。当程序启动时,它需要读取文件并将所有当前的汽车放入一个数组中,以便显示它们。然后,程序的其余部分将做其他事情,如删除汽车和添加新闻等。我所在的部分是当程序首次启动它需要阅读文件,但我似乎无法得到它的工作。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