0

For an assignment, I was asked to work on calling a class and creating an array objects, which i did here;

public void DVDArrayObjects() {
    //creates variables
    int i;
    DVDClass[] dvdArray = new DVDClass[5];

    //reference to DVDClass
    for (i = 0; i < 2; i ++) {
        //create new instance of calling the class
            dvdArray[i] = new DVDClass();

        //create new instance of getting the info
            dvdArray[i].getDVDInfo();

        //display
        //System.out.println(dvdArray[i]);
    }
}

Creating the array of objects works fine, but displaying doesn't. it shows the memory allocation when i run it. I'm really stuck as to how to get it to display.

** EDIT **

When i use System.out.println(dvdArray[i].getDVDInfo()); the error void types not allowed in here shows up

** END OF EDIT **

Any help at all would be greatly appreciated.

Megan Sime
  • 1,257
  • 2
  • 16
  • 25

3 Answers3

2

Print the DVD info (assuming that it returns a string).

System.out.println(dvdArray[i].getDVDInfo());

If it doesn't return a string, you need to override the toString() method on the class DVDInfo like this.

@Override
public String toString()
{
    return "Film Name\t: " + filmName +
           "\nFilm Director\t: " + filmDirector +
           "\nRun Time\t: " + runTime +
           "\nLead Actor\t: " + leadActor;
}

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
1

You need to override the toString() method.

public class DVDCLass {

    @Override
    public String toString(){
        return // whatever you want the output to be
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Override toString() method in your DVDClass class

do like below

class DVDClass{

     public String toString(){
          return // whatever you want the output to be
     }
}
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64