Java:读取文本文件,然后转换
我遇到了一个问题,获取读取文件的方法,然后将其转换为整数。以下是对该计划的简要说明。它本质上是一个汽车经销商库存,通过将车辆记录在文本文件中来跟踪该批次中的车辆。当程序启动时,需要读取文件并将所有当前车辆放入一个数组中,以便显示它们。然后,程序的其余部分将做其他事情,如删除汽车和添加新闻等。我所处的部分是当程序首次启动时,它需要读取文件,但我似乎无法让它工作 文本文件共6行;先是4个数字,然后分别是2个单词。我希望该方法读取前四行并将其转换为整数,然后将它们存储在临时数组中。然后,它将读取接下来的两行,并将它们存储在一个临时数组中。之后,我获取所有这些存储值并将它们发送给构造函数。然后,构造函数存储在Arraylist中,Arraylist可以随时访问。在输出中,它可以很好地完成所有这一切。但它希望第二次使用这种方法,尽管存在防止这种情况发生的障碍 这是代码。这是一门课,不是主程序。我会尽量在代码中解释程序Java:读取文本文件,然后转换,java,readfile,Java,Readfile,我遇到了一个问题,获取读取文件的方法,然后将其转换为整数。以下是对该计划的简要说明。它本质上是一个汽车经销商库存,通过将车辆记录在文本文件中来跟踪该批次中的车辆。当程序启动时,需要读取文件并将所有当前车辆放入一个数组中,以便显示它们。然后,程序的其余部分将做其他事情,如删除汽车和添加新闻等。我所处的部分是当程序首次启动时,它需要读取文件,但我似乎无法让它工作 文本文件共6行;先是4个数字,然后分别是2个单词。我希望该方法读取前四行并将其转换为整数,然后将它们存储在临时数组中。然后,它将读取接下来
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();
}
我不能让它在循环结束时读,因为如果它读了,而且在我刚读的那辆车之后还有更多的车,当循环重新启动时,它进入下一行,所有的东西都被抛出
感谢您的帮助,谢谢 在for
循环中使用标记的break语句,当myline
变为null
时,只需退出主do而循环。在循环中实例化其他对象的方式不会为轻松重构留下太多空间,因此在这里使用带标签的break是有意义的
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);
outerloop:
做{
对于(int i=0;i<4;i++){
如果((myline=readFile.readLine())==null)中断outerloop;
// ..
}
对于(int i=0;i<2;i++){
如果((myline=readFile.readLine())==null)中断outerloop;
// ..
}
// ..
}while(myline!=null);
在for
循环中使用标记的break语句,当myline
变为null
时,只需退出主do而循环即可。在循环中实例化其他对象的方式不会为轻松重构留下太多空间,因此在这里使用带标签的break是有意义的
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);
outerloop:
做{
对于(int i=0;i<4;i++){
如果((myline=readFile.readLine())==null)中断outerloop;
// ..
}
对于(int i=0;i<2;i++){
如果((myline=readFile.readLine())==null)中断outerloop;
// ..
}
// ..
}while(myline!=null);
也许您可以使用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
也许您可以使用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
如果您有一个最小的、可编译的类,并告诉我们您的预期输出与实际输出,那么您的问题将更简洁、更容易回答。我现在希望作为输出得到的正是文本文件中的内容。现在,我得到文本文件中的内容加上另外6行null。你确定文件底部没有空行吗?您是否考虑添加检查以及null?还是应该允许空行?现在文件上没有空位。我只是想找个方法让它读下一行,看看它是否为空,如果不是,返回到前一行。如果你有一个最小的,编译一个可编译的类,并告诉我们您的预期输出与实际输出。我现在希望得到的输出正好是文本文件中的内容。现在,我得到文本文件中的内容加上另外6行null。你确定文件底部没有空行吗?您是否考虑添加检查以及null?还是应该允许空行?现在文件上没有空位。我真的只是在寻找一种方法,让它读取下一行,看看它是否为空,如果不是,返回到之前的行。我尝试了这个,它成功了!谢谢虽然我有一个关于外环的问题。当你使用它时,它是否只与它下面的循环有关?再次感谢。是的,这就像给它下面的循环一个标签(标签可以是任何文本,比如mainloop等等)。但是,我建议您将与一辆车相关的所有数据保存在文件中的同一行(以逗号分隔的值)。然后,您可以通过检查while(reader.readLine()!=null)
和line.split(“,”)
每一行来处理值,从而简单地检查EOF(文件结尾)。我尝试了这一点,效果很好!谢谢虽然我有一个关于外环的问题。当你使用它时,它是否只与它下面的循环有关?再次感谢。是的,这就像给它下面的循环一个标签(标签可以是任何文本,比如mainloop等等)。但是,我建议您将与一辆车有关的所有数据保存在同一台机器上