简体   繁体   English

从文本文件中读取数据并将其存储在对象中

[英]Reading data from a text file and storing it in an object

I have a text file with a number on each line. 我有一个文本文件,每行都有一个数字。

0 
55
3
15
63
8
0
-8
9
89
504
32

I have a Car which accepts three parameters: 我有一辆Car可以接受三个参数:

  • the starting odometer reading 起始里程表读数
  • the final odometer reading 最后的里程表读数
  • the litres

The first line in the text file corresponds to the starting odometer reading. 文本文件中的第一行对应于起始里程表读数。
The second is the final reading. 第二个是最终阅读。
The third is the litres. 第三是升。
The fourth is the starting odometer reading for the second Car , etc. 第四个是第二起始里程表读数Car

I need to read the text file, create an object, and this the parameters to the car. 我需要读取文本文件,创建一个对象,以及这个参数给汽车。

For car3 (0, -8, 9) there is a negative number, so the entire set is ignored and (89, 504, 32) becomes car3 . 对于car3 (0, -8, 9)存在负数,因此忽略整个集合,并且(89, 504, 32) car3 (89, 504, 32)变为car3

I have referred to Anubian Noob's answer ; 我提到了Anubian Noob的答案 ; and this is my code so far: 到目前为止这是我的代码:

final String INPUT_FILE = "data.txt";
final String OUTPUT_FILE = "report.txt";

BufferedReader inputFile = new BufferedReader (new FileReader (INPUT_FILE)); 
BufferedWriter outputFile = new BufferedWriter (new FileWriter (OUTPUT_FILE));
LineNumberReader  lineNumber = new LineNumberReader (new FileReader (INPUT_FILE));
lineNumber.skip(Long.MAX_VALUE);
int length = lineNumber.getLineNumber();
lineNumber.close();


String line = inputFile.readLine();

Car[] car = new Car[length/3];

while (line != null)
{
    for (int i = 0; i < length/3; i += 3) 
    {
        int startReading = Integer.parseInt(inputFile.readLine());
        int endReading = Integer.parseInt(inputFile.readLine());
        int liter = Integer.parseInt(inputFile.readLine());
        car[i] = new Car (startKm, endKm, litre);
    }
}
inputFile.close();
outputFile.close();

On line int liter = Integer.parseInt(inputFile.readLine()); 在线int liter = Integer.parseInt(inputFile.readLine()); I get the following error: 我收到以下错误:

java.lang.NumberFormatException: null 
null (in java.lang.Integer)

How do I store the three pieces of information into its respective object? 如何将三条信息存储到各自的对象中?

*Note: There isn't a set amount of lines in the text file, and we have to use an array. *注意:文本文件中没有设定数量的行,我们必须使用数组。

It's because you are reading the first line of the file, and not using it; 这是因为你正在阅读文件的第一行,而不是使用它; you're starting with the second line of the file and assign it to startReading of the first car. 你从文件的第二行开始,并将其分配给第一辆汽车的startReading So, you won't have enough lines in the file (you counted the number of lines, and hence the number of cars, in the file first, but you are reading one line too many) 所以,你不会在文件中有足够的行数(你先计算文件中的行数,从而计算出汽车数量,但是你读的行数太多了)

Also, your loop shouldn't increase i with 3, because you've already divided the number of lines by 3. And you're using i as the index into the car array. 此外,你的循环应该不会增加i有3,因为你已经除以3线的数量和你使用i的索引, car阵。

Change the code to: 将代码更改为:

lineNumber.close();

// REMOVE String line = inputFile.readLine();

Car[] car = new Car[length/3];

// REMOVE while (line != null)
// REMOVE {
for (int i = 0; i < length/3; i ++) // DON'T DO i += 3 because that will make you go beyond the bounds of the car array 
{
    int startReading = Integer.parseInt(inputFile.readLine());
    int endReading = Integer.parseInt(inputFile.readLine());
    int liter = Integer.parseInt(inputFile.readLine());
    car[i] = new Car (startKm, endKm, litre);
}
// REMOVE }

The problem with the code is with the nested loop below: 代码的问题在于下面的嵌套循环:

String line = inputFile.readLine();
Car[] car = new Car[length/3];

while (line != null)
{
    for (int i = 0; i < length/3; i += 3) 
    {
        int startReading = Integer.parseInt(inputFile.readLine());
        int endReading = Integer.parseInt(inputFile.readLine());
        int liter = Integer.parseInt(inputFile.readLine());
        car[i] = new Car (startKm, endKm, litre);
    }
}

Since you're reading the first line for inputFile to initialize line variable, all your further car value reads are off by 1. In addition, since you're never reassign line variable it will be always not null (unless you read an empty file) and will create infinite loop. 由于您正在读取inputFile的第一行以初始化line变量,因此您的所有进一步的汽车值读数都会被1关闭。此外,由于您永远不会重新分配line变量,因此它始终不为空(除非您读取空文件)并将创建无限循环。

Removing the line variable alltogether with the external loop should resolve the problem, since you're already have the condition based on the number of lines in the file. 使用外部循环一起删除行变量应该可以解决问题,因为您已经拥有基于文件中行数的条件。

Why don't you create a class named car: 你为什么不创建一个名为car的类:

public Class Car{
  private int starting_odometer;
  private int final_odometer;
  private int liters;
  //Constructor
  //Getters and setters
}

And the read the file and set the lines to the right attributes: 并读取文件并将行设置为正确的属性:

    Car myCar = new Car();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int count=1;
while ((line = br.readLine()) != null) {
if(count<=3)
{    
    if(count==1){
    myCar.setStarting_odometer(parseInt(line);
    }
    if(count==2){
    myCar.setFinal_odometer(parseInt(line);
    }
    if(count==3){
    myCar.setLiters(parseInt(line);
    }
    count++;
}
else {count=1;}    

}
br.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM