943,078 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1649
  • Java RSS
Dec 14th, 2009
0

Creating Object Array and then printing

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

java Syntax (Toggle Plain Text)
  1. public class Video {
  2.  
  3. String videoTitle;
  4. int videoRating;
  5.  
  6. public Video (String pVideoTitle, int pVideoRating){
  7. videoTitle = pVideoTitle;
  8. videoRating = pVideoRating;
  9. }
  10. }

java Syntax (Toggle Plain Text)
  1. public class VideoStore {
  2.  
  3. public VideoStore(){
  4. run();
  5. }
  6. private void run(){
  7. buildArray();
  8. printArray();
  9. }
  10. private void buildArray(){
  11. Video[] inventory = new Video[5];
  12. inventory[0] = new Video ("Meet the Fockers", 5);
  13. inventory[1] = new Video ("Blade" , 3);
  14. inventory[2] = new Video ("Spiderman 2 " , 4);
  15. inventory[3] = new Video ("Finding Nemo" , 3);
  16. inventory[4] = new Video ("Star Wars II" , 1);
  17. }
  18. private void printArray(inventory[]){
  19. for (int i = 0; i < inventory.length; i++){
  20. System.out.println(inventory[i]);
  21. }
  22. }
  23. public static void main(String[] args) {
  24. new VideoStore();
  25. }
  26. }

I am getting an error in the printArray method. It does not like how I reference the inventory array.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AZSooner is offline Offline
3 posts
since Dec 2009
Dec 14th, 2009
0
Re: Creating Object Array and then printing
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:
Java Syntax (Toggle Plain Text)
  1. private void printArray(inventory[]){
  2. for (int i = 0; i < inventory.length; i++){
  3. System.out.println(inventory[i]);
  4. }
  5. }

What it should be:
Java Syntax (Toggle Plain Text)
  1. private void printArray(Video[] inventory){
  2. for (int i = 0; i < inventory.length; i++){
  3. System.out.println(inventory[i]);
  4. }
  5. }

Does that help?
-Michael
Reputation Points: 11
Solved Threads: 7
Newbie Poster
Mikeyp926 is offline Offline
15 posts
since Oct 2009
Dec 14th, 2009
0
Re: Creating Object Array and then printing
Click to Expand / Collapse  Quote originally posted by Mikeyp926 ...
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:
Java Syntax (Toggle Plain Text)
  1. private void printArray(inventory[]){
  2. for (int i = 0; i < inventory.length; i++){
  3. System.out.println(inventory[i]);
  4. }
  5. }

What it should be:
Java Syntax (Toggle Plain Text)
  1. private void printArray(Video[] inventory){
  2. for (int i = 0; i < inventory.length; i++){
  3. System.out.println(inventory[i]);
  4. }
  5. }

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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AZSooner is offline Offline
3 posts
since Dec 2009
Dec 14th, 2009
0
Re: Creating Object Array and then printing
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.

java Syntax (Toggle Plain Text)
  1. private buildArray(){
  2. Video[] inventory = new Video[5];
  3. inventory[0] = new Video ("Meet the Fockers", 5);
  4. inventory[1] = new Video ("Blade" , 3);
  5. inventory[2] = new Video ("Spiderman 2 " , 4);
  6. inventory[3] = new Video ("Finding Nemo" , 3);
  7. inventory[4] = new Video ("Star Wars II" , 1);
  8. return Video [] inventory;
  9. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AZSooner is offline Offline
3 posts
since Dec 2009
Dec 14th, 2009
0
Re: Creating Object Array and then printing
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
Reputation Points: 11
Solved Threads: 7
Newbie Poster
Mikeyp926 is offline Offline
15 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Binery tree help
Next Thread in Java Forum Timeline: Overriding toString()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC