The actual code works to encode values to the properties of each object within the array.
However, it’s using the information from the last line only to populate each value.
Scanner data = new Scanner(new DataInputStream(new FileInputStream("trucklist.txt")));
for (int i = 0; i < maxSize; i++)
{ for(;data.hasNext();)
{
t[i].setVin(data.next());
t[i].setMake(data.next());
t[i].setModel(data.next());
t[i].setColor(data.next());
t[i].setYear(data.next());
t[i].setBaySize(data.nextDouble());
t[i].setCabSize(data.nextDouble());
addElement(t[i]);
}
}
where trucklist.txt is :
12x4578w Ford F-150 Black 2005 17.2 6.8
12x4589w Ford F-150 Blue 2005 17.2 6.8
12x4587f Ford F-250 Black 2007 60.8 9.5
12x4589z Ford F-150x White 2005 15.4 10.2
The array elements are only being populated by the last set of properties for each element. Any help would be greatly appreciated.
As this is currently written,
iwon’t increment while there is still data to be read. Try this instead:As a side note, this routine is very dependent upon a correctly formatted data file, and will throw unchecked exceptions if the data is malformed.