0

Goal: Add a new Movie object to an existing Movie[] if there is room to add.

Code:

    // Create the new Movie object
    Movie movieToAdd = new Movie (newTitle, newYear);

    // Add it to the Array
    count = addMovie(movieList, movieToAdd, count);

Method Code:

    public static int addMovie (Movie[] movieArray, Movie addMe, int count)
{
    if (count != movieArray.length)
    {
        count++;
        movieArray[count] = addMe;
        System.out.println("Movie added successfully!");
    }
    else
    {
        System.out.println("Array size of " + movieArray.length + " is full. Could not add movie.");
    }


    return count;
}

QUESTION: Currently, when the movieList array is printed out, the new entry prints as null even though the created Movie object will print just fine outside of the way. Therefore, I'm assuming the best way to add the addMe object into the array is to create a second new Movie object initialized within the array and build it piece by piece (so addMe will remain in memory, and a "copy" of addMe will be set into the array).

This to me doesn't feel very efficient (I hate extra data laying about...). Is there a better way to do this?

NOTE: The Movie object actually has 10 private data members. For this exercise I only needed to pass in two parameters and set defaults for the rest. You can imagine why I don't to use ten GET statements to build this array and have extra objects stuck in memory...

EDIT: Current Print Out (Portions):

    Menu options:
1.  Show all movies: 
2.  Show movies sorted - manual
3.  Show movies sorted - auto
4.  Show Movie by Index
5.  Search for movie Linearly
6.  Search for movie using Binary Search
7.  Add a movie
20. Quit 
Please choose an option from the menu: 1 to 20: 
7
Let's add the information for the new movie. Give me a Title and 4-digit Year, and I'll fill in the rest.
Title? 
Me
Year of Release? 
Please enter a valid 4 digit year: 1000 to 9999: 
1213
Movie added successfully!
Menu options:
1.  Show all movies: 
2.  Show movies sorted - manual
3.  Show movies sorted - auto
4.  Show Movie by Index
5.  Search for movie Linearly
6.  Search for movie using Binary Search
7.  Add a movie
20. Quit 
Please choose an option from the menu: 1 to 20:




25 | Les Vampires (1915)                   | Louis Feuillade                               | "Edouard Mathe, Marcel Levesque"                                                                                                                                                          | 1915 |   0 | http://www.imdb.com/title/tt0006206/          | http://www.guardian.co.uk/film/movie/117077/vampires                               | France       | Horror               | 175
null | 176
=============================================================================

MORE EDITS: Constructor and Setters code - all this SHOULD be working right though.

    public Movie (String t, int y)
{
//  passed in
    this.title = setTitle(t);
    this.year = setYear(y);

//  defaults
    this.ranking = 0;
    this.director = "No Director";
    this.actors = "No Actors";
    this.oscars = 0;
    this.linkIMDB = "No IMDB Link";
    this.linkGuardian = "No Guardian Link";
    this.country = "No Country";
    this.genre = "No Genre";    
}

    public String setTitle (String newTitle)
{       
    if (newTitle == null)
    {
        this.title = "No Title";
    }
    else
    {
        this.title = newTitle;
    }

    return this.title;
}

    public int setYear (int newYear)
{
    if (newYear >= 999 && newYear <=10000)
    {
        this.year = newYear;
    }
    else
    {
        newYear = 0000;
    }

    return this.year;
}
14
  • "when the movieList array is printed out, the new entry prints as null" What? Where? How? This code looks fine. Commented Oct 12, 2013 at 3:15
  • 2
    Yuriy System.out.println is a good hint ;) Commented Oct 12, 2013 at 3:16
  • 1
    why are you incrementing the count after checking to see if it's not equal to the length of the array? Commented Oct 12, 2013 at 3:18
  • 1
    That is not the problem. By that line of thinking the new operator also just returns a "memory address" and not the object itself, so what is the point of copying? I think this may be an issue that you'll have to pry into yourself. Construct simpler test cases and set breakpoints in the code and try to see why you're getting null. Commented Oct 12, 2013 at 3:53
  • 1
    Well, a correct search should not raise any exceptions so that doesn't really say much about the array. I really think you are making too many assumptions. Better to be sure and make use of the debugging tools of your IDE. Commented Oct 12, 2013 at 4:23

2 Answers 2

1

It isn't clear what you are asking, but this portion is incorrect:

count++;
movieArray[count] = addMe;

What if movieArray.length is 10, and count is 9? Then it will pass the count != movieArray.length check and then you will try to assign the element at index 10. Use post increment:

movieArray[count++] = addMe;
Sign up to request clarification or add additional context in comments.

7 Comments

Count will always be less than the array size as it's a number generated when movies are initially read in from a file. See above comments.
@ChristinaKline That is not the only consequence; your code will also never assign to the first element at index 0.
This program starts by loading in 176 movies from eight different files. I simply want to be able to add to this list. The assignment is to first create an object and then pass that object into a method that will add it to the existing list of movies.
It prints out correctly - except the added movie prints out as Null instead of printing the information. I've added a snippet from the output. that 176 is the new index number - it is correct. But the information before it is null.
GOT IT! I was using Count to set the index at which the new movie was stored. Original count was 176. Last index was 175. I was increment BEFORE setting the movie, so the movie was being set at index 177. So 176 was getting skipped. It was only printing to 176 because that was the actual count, which wasn't accounting for the skipped space. (Figured this out when I attempted adding 2 new Movie objects to the array and got a null and then the first object only). Solved by switching the set and the increment.
|
0

GOT IT!

I was using Count to set the index at which the new movie was stored. Original count was 176. Last index was 175. I was increment BEFORE setting the movie, so the movie was being set at index 177. So 176 was getting skipped.

It was only printing to 176 because that was the actual count, which wasn't accounting for the skipped space (there was an extra object in the array that wasn't getting printed).

(Figured this out when I attempted adding 2 new Movie objects to the array and got a null and then the first object only on print).

Solved by switching the set and the increment:

if (count <= movieArray.length)
    {
        movieArray[count] = addMe;
        count++;
        System.out.println("Movie added successfully!");
    }

1 Comment

This has the same effect as @ZongZhengLi's answer, because the value of count++ is count's old value. This is the point of the post-increment operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.