Program not printing element at index 0

Tags: , ,



I’m working on case 3 and I need to print some details about the 3 instances for the Airplane class and the BatMobile class. The loop only prints out elements at index 1 and 2 and the other 3 instances for the BatMobile class. Why is this happening and how can I print all 3 instances for both classes?

import java.util.Scanner;

public class vehicleInstancesAndMenu {
static int count;
Vehicle[] vehicles = new Vehicle[15];
public vehicleInstancesAndMenu() {
    menu();
}//constructor

public void vehicles_Instances() {
    
    vehicles[0] = new Airplane("Airbus A220-100", "Delta Airlines", "2019", 2, 120, 300, 221, 250);
    vehicles[1] = new Airplane("Airbus A319-100", "Delta Airlines", "2018", 2, 200, 450, 319, 300);
    vehicles[2] = new Airplane("Airbus A320-200", "Delta Airlines", "2018", 2, 220, 500, 320, 320);
    
    vehicles[3] = new Automobile("Corolla LE", "Toyota", "2020", 300, 6, 150);
    vehicles[4] = new Automobile("Civic SE", "Honda", "2018", 310, 6, 160);
    vehicles[5] = new Automobile("Sentra", "Nissan", "2019", 360, 6, 200);
    
    vehicles[6] = new Ship("APM - Maersk", "Maersk", "1904", 400000, "Carnival",2695, 35);
    vehicles[7] = new Ship("MSC", "Mediterranean Shipping Company", "1970", 30000 ,"Mediterranean Shipping Company",2565, 32);
    vehicles[8] = new Ship("COSCO", "China Ocean Shipping Company", "1990", 650000, "China Ocean Shipping Company",36201, 40);
    
    vehicles[9] =  new Tesla("Tesla Model 3", "Tesla", "2020", 1, 250, 200, 6, 300);
    vehicles[10] = new Tesla("Tesla Model S", "Tesla", "2020", 1, 300, 220, 6, 390);
    vehicles[11] = new Tesla("Tesla Model Y", "Tesla", "2020", 1, 400, 360, 6, 420);
    
    vehicles[12] = new BatMobile("Model A","Wayne Industries", "2020", 4 , 300, 600 , 40000, "Wayne Industries", 500, 320);
    vehicles[13] = new BatMobile("Model C", "Wayne Industries", "2019", 3 , 290, 400, 32000, "Wayne Industries", 450, 310);
    vehicles[14] = new BatMobile("Model T", "Wayne Industries", "2018",3 , 250, 360, 280000, "Wayne Industries", 370, 310);

    count = vehicles.length;
}

public void menu() {
    Scanner scan = new Scanner(System.in);
    String response;
    
    System.out.println("Welcome! n1 - To see how many vehicles are in the systemn2 - To see the name and the class of each vehiclen3 - To see which vehicles can fly.n4 - To see which vehicles can float.n5 - To see which vehicles can fly and float.n6 - To see a description of each vehicle.nh - to see brief help information about the system.nq - To terminate the program.");
        response = scan.nextLine();
    
    switch(response) {
        case "1":
            if(response.equals("1")) {
                vehicles_Instances();
                System.out.println("There are "+vehicleInstancesAndMenu.count+" vehicles in the system.");  
            }
            
        case "2":
            if(response.equals("2")) {
                for(int i = 0; i< vehicles.length;i++) {
                    vehicles_Instances();
                    System.out.println( "Name: "+vehicles[i].getName()+" Class: "+vehicles[i].getClass().getSimpleName()+"n" );
                }   
            }
            
        case "3":
            if(response.equals("3")) {
                
                Vehicle airplane = new Airplane(response, response, response, 0, 0, 0, 0, 0);
                Vehicle batMobile = new BatMobile(response, response, response, 0, 0, 0, 0, response, 0, 0);
                
                for(int i = 0; i < vehicles.length; i++) {
                    if( (vehicles[i] instanceof Airplane) || (vehicles[i] instanceof BatMobile) ){
                        System.out.println( "Name: "+vehicles[i].getName() + "tType: "+vehicles[i].getClass().getSimpleName() );
                        System.out.println();
                    }else {
                    }
                    vehicles_Instances();
                }
            }

Answer

Since the array is initialized with null values the first time you run

`if( (vehicles[i] instanceof Airplane) || (vehicles[i] instanceof BatMobile) )`

it returns false and runs vehicles_Instances() which fills the array but then the for loop increments so it will print index 1.

so just run vehicles_Instances(); before the for loop



Source: stackoverflow