简体   繁体   中英

Java Class access from main

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.

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?

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

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
}

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