简体   繁体   中英

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:

  • 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.

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 .

I have referred to Anubian Noob's answer ; 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()); 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. 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.

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.

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:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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