I'm trying to print an array of objects.
Also numOfVehicles seems to be incrementing because when I initialize it to anything above 8, it prints the number plus 8. If numOfVehicles is not initialized of initialized to something less than 9, it gives an ArrayIndexOutOfBoundsException.
When I'm able to print with out a run-time error, the array just prints null. How do I print the entire array?
I also have a toString method in the Vehicle class. I have a FleetApp class as well.
Here's whats in the input files.
1256BB03457
Hyundai
Santa Fe
2011 18556.0 31195 27301
true true 5
5XXCD3AX66
Kia
Sorento
2005 9400.0 67124 60245
false false 5
9SEF3AX56M
FORD
TAURUS 3.5L
2001 5400.0 53892 50753
false true 5
59867451W
Honda
Odyssey Touring Elite
2011 35000.0 7500 7500
true true 7
ADF2462622F
Chevrolet
Tahoe
2007 35000.0 35194 24301
false 9
B288413D444
Ford
Expedition
2013 43000.0 5500 3500
false 8
GKS61162226
GMC
Yukon Hybrid
2004 51000.0 210000 209000
true 5
7865K162563
Toyota
Sequoia
2004 51000.0 210000 209000
false 8
public class Fleet { Vehicle[] vhcl; private int numOfVehicles; public Fleet() { vhcl = new Vehicle[numOfVehicles]; } public void read(File input1, File input2) { String vin = null, make = null, model = null; int year = 0, i = 0, ctr = 0, passCap; double value = 0, totalMilesDriven = 0, lastOilChange = 0; boolean antiTheft, stability; try { Scanner in1 = new Scanner(input1); while (in1.hasNext() && i < vhcl.length) { vin = in1.nextLine(); make = in1.nextLine(); model = in1.nextLine(); year = in1.nextInt(); value = in1.nextDouble(); totalMilesDriven = in1.nextDouble(); lastOilChange = in1.nextDouble(); antiTheft = in1.nextBoolean(); stability = in1.nextBoolean(); passCap = in1.nextInt(); in1.nextLine(); vhcl[i] = new Vehicle(vin, make, model, year, value, 'c', totalMilesDriven, lastOilChange); i++; numOfVehicles++; } in1 = new Scanner(input2); while (in1.hasNext() && i < vhcl.length) { vin = in1.nextLine(); make = in1.nextLine(); model = in1.nextLine(); year = in1.nextInt(); value = in1.nextDouble(); totalMilesDriven = in1.nextDouble(); lastOilChange = in1.nextDouble(); antiTheft = in1.nextBoolean(); passCap = in1.nextInt(); in1.nextLine(); vhcl[i] = new Vehicle(vin, make, model, year, value, 'c', totalMilesDriven, lastOilChange); i++; numOfVehicles++; } System.out.println(vhcl[i]); System.out.println(numOfVehicles); } catch (FileNotFoundException e) { System.out.println("The file was not found."); } } }