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.