0
public class mainTest {
    public static void main(String[] args) throws Exception {

        Scanner KB = new Scanner(System.in);
        String VehiclesFile = "Vehicles.txt";

        File file1 = new File(VehiclesFile);
        Scanner infile1 = new Scanner(VehiclesFile);
        Vehicle[] Vehicles = new Vehicle[0];

        try {
            Scanner scanner = new Scanner(file1);
            int lineCount = 0;
            while (scanner.hasNextLine()) {
                lineCount++;
                scanner.nextLine();
            }

            Vehicles = new Vehicle[lineCount];

            scanner = new Scanner(file1);
            int VehicleCount = 0;
            while (scanner.hasNextLine()) {
                String[] temp1 = scanner.nextLine().split(",");

                // file has been read into temp1[] now to use Vehicles
                // class type

                Vehicles[VehicleCount] = new Vehicle();
                Vehicles[VehicleCount].setregistration(temp1[0]);
                Vehicles[VehicleCount].setmake(temp1[1]);
                Vehicles[VehicleCount].setModel(temp1[2]);
                Vehicles[VehicleCount].setyear(temp1[3]);
                Vehicles[VehicleCount].setodometer(temp1[4]);
                Vehicles[VehicleCount].setowner(temp1[5]);
                VehicleCount++;

            }
        } catch (IOException e) {
            // Print out the exception that occurred
            System.out.println("Unable to find ");
        }

//*******This is where I need to access the class to print****************
        System.out.println (Vehicle.class.getClasses());

    }
}

I cannot seem to understand how to reference a specific part of the class/array of class objects The class for Vehicle is in defined with get/set so I didn't include the code.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326

3 Answers3

1
System.out.println(Arrays.toString(Vehicles));

Make sure that the vehicle class has toString() method overriden. Otherwise it will just print out the references.

See: How to override toString() properly in Java?

Community
  • 1
  • 1
Maxaon3000
  • 1,109
  • 8
  • 15
0

If you want to print off data from the Vehicle objects you'll have to loop through that array and call the getter methods you mentioned before. It should be something like

for(Vehicle v : Vehicles)
{
     System.out.print(v.getYear() + " " + v.getMake() + " " + v.getModel());
}
Anthony Porter
  • 152
  • 1
  • 9
0

Seems to me like you're mixing up the concept of classes and objects. Class is short for classification, so a class is a type of something. An object is a single instance of a class. So it's a single item of a certain type.

What you have is an array of objects, not classes, and you want to print the information of each object. So say you have five vehicles in your array, you will have to call the function System.out.println(/*data to print*/) five times. One for each element in the array.

To omit repetition, you can use a loop:

for (int index = 0; index < Vehicles.length; ++i) {
    System.out.println(Vehicle[index].getMake());
    // do the same to print other attributes of the Vehicle class
}
Alex
  • 3,111
  • 6
  • 27
  • 43
  • Thank you, I am still trying to understand how to properly use classes, and probably confused myself reading about object oriented notions. Your code results in Null being printed. Could there be an issue with the way my code writes into the Vehicles array? – Geordie Todd Oct 23 '14 at 00:37
  • Does it print a mix of `null`s and proper data, or does it print some data and then followed by all `null`s? If the 1st case, then it's likely that your `setX()` methods are not implemented correctly, or something is wrong with the string you're reading in and splitting. In the 2nd case, it's because the array is larger than the amount of items you have stored in it. Say you have an array of size 10, but only 5 vehicles. You'll end up with 5 vehicles, and 5 `null`s printed. I can give you more details if you update your question with a sample output – Alex Oct 23 '14 at 02:16
  • It prints null for all instances, (35 lines of null). The array size is set by the lineCountwhich is declared after the infile and lineCount++ for each new line in file. When I use System.out.println(Arrays.toString(temp1)); prior to the Vehicles[VehicleCount] = new Vehicle(); ... and all the [ ]get s .... It prints fine, but I need to access and print once the while (scanner.hasNextLine()) is closed "}" – Geordie Todd Oct 23 '14 at 03:04
  • I'm not 100% sure on this, but the only thing I can think of is the 2nd while loop. `scanner.hasNextLine()` could be giving you a `false`, which causes each element in the array to stay all `null`s – Alex Oct 23 '14 at 16:25