I am creating a Video class, then creating objects to place into an array. Then I want to print the contents of the array:

public class Video {

    String videoTitle;
    int videoRating;

    public Video (String pVideoTitle, int pVideoRating){
        videoTitle = pVideoTitle;
        videoRating = pVideoRating;
    }
}
public class VideoStore {

    public VideoStore(){
        run();
    }
    private void run(){
        buildArray();
        printArray();
    }
    private void buildArray(){
        Video[] inventory = new Video[5];
        inventory[0] = new Video ("Meet the Fockers", 5);
        inventory[1] = new Video ("Blade" , 3);
        inventory[2] = new Video ("Spiderman 2 " , 4);
        inventory[3] = new Video ("Finding Nemo" , 3);
        inventory[4] = new Video ("Star Wars II" , 1);
    }
    private void printArray(inventory[]){
        for (int i = 0; i < inventory.length; i++){
        System.out.println(inventory[i]);
        }
    }
    public static void main(String[] args) {
      new VideoStore();
    }
}

I am getting an error in the printArray method. It does not like how I reference the inventory array.

Recommended Answers

All 4 Replies

It doesn't like the way you reference the inventory array because in your method signature you don't properly state what the parameter is.

Your code:

private void printArray(inventory[]){
for (int i = 0; i < inventory.length; i++){
System.out.println(inventory[i]);
}
}

What it should be:

private void printArray(Video[] inventory){
for (int i = 0; i < inventory.length; i++){
System.out.println(inventory[i]);
}
}

Does that help?
-Michael

It doesn't like the way you reference the inventory array because in your method signature you don't properly state what the parameter is.

Your code:

private void printArray(inventory[]){
for (int i = 0; i < inventory.length; i++){
System.out.println(inventory[i]);
}
}

What it should be:

private void printArray(Video[] inventory){
for (int i = 0; i < inventory.length; i++){
System.out.println(inventory[i]);
}
}

Does that help?
-Michael

Yes, that does help. Wasn't sure how to handle an arrary as a parameter (not as straight forward as an int or String)

I have changed the buildArray method to the below, as I realized that I shouldn't have a void in the method since I have an output. But I am having trouble with the return.

private buildArray(){
Video[] inventory = new Video[5];
inventory[0] = new Video ("Meet the Fockers", 5);
inventory[1] = new Video ("Blade" , 3);
inventory[2] = new Video ("Spiderman 2 " , 4);
inventory[3] = new Video ("Finding Nemo" , 3);
inventory[4] = new Video ("Star Wars II" , 1);
return Video [] inventory;
}

Ok, I think I see what you are trying to do. I would recommend that you leave the method returning void, and made your array a member variable of your class. Does that make sense? You won't actually return anything from your buildArray method, but you would create the array that is stored as a member variable and add items to it. You'll have to change the structure of your code a little bit, but it shouldn't be too much work.

-Michael

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.