1

I have a program in which I need to print out several Movie objects (represented just by a single String) in an array. I start with a text file that already has five movies inside of it, but in the console, I am to allow the user to expand the array if he/she wants. (I cannot use an arraylist in this problem). I've attempted to design a program that does this, but I get an out of bounds exception each time I try to add a new movie. I also need to check the array to see if there is a duplicate movie object inside it? How can I do that?

Question: How can I allow the user to expand the array and add more movies to the list? How can I check the array to see if there is already a certain movie title inside of it?

public class MovieDriver {

    //variable declaration
    static Movie[] movies = new Movie[5];
    static Scanner scan = new Scanner(System.in);
    static int input = 0;
    static String title = "";

    public static void main(String[] args) throws FileNotFoundException {

        //retrieves movie data
        getData();

        System.out.println("Welcome to the favorite movie program.");

    do{ 
        System.out.println("Press 1 to print the list, 2 to add another movie, 3 to end the program.");

        input = scan.nextInt();     

        switch(input) {

        case 1: 
            for(int i = 0; i < movies.length; i++)
            {
            System.out.println(movies[i].toString());
            }

            break;

        case 2:
            System.out.println("Please enter the movie you would like to add to the list:");
            title = scan.nextLine();
            movies = Arrays.copyOf(movies, movies.length+1);
            movies[movies.length] = new Movie(title);

            break;

        case 3:
            System.out.println("Program terminated.");

            break;



            }
        } while (input != 3);
    }

    // method to retrieve data
    public static void getData() throws FileNotFoundException {
        // reads in movie data
        File MovieData = new File("./src/Movies.txt");
        Scanner fileScanner = new Scanner(MovieData);
        int i = 0;

        // while there is a new line in the data, goes to the next one
        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            Scanner lineScanner = new Scanner(line);
            String title = lineScanner.nextLine();

            // creates a movie
            movies[i] = new Movie(title);
            i++;
        }
    }
}
  • Just so you know you cannot "expand" the size of an array. You are actually destroying the original array and creating an entirely new one. – CodeCamper Mar 15 '15 at 19:07
  • Rule of thumb: if you have a known quantity of elements to store in your data structure, use array. If you need to change the the number of elements or don't know the number of elements you'll have, use ArrayList. – MarsAtomic Mar 15 '15 at 19:15

3 Answers3

1

first question: IndexOutOfBound

No need to use System.arraycopy(). You use the right method with Arrays.copyOf(). The problem comes from

movies = Arrays.copyOf(movies, movies.length+1); 
movies[movies.length] = new Movie(title);  // <--- here

After you call copyOf(), the new length of the array is 6. And when you do movies[movies.length]=... you try to access the 7th element. Just do:

movies = Arrays.copyOf(movies, movies.length+1); 
movies[movies.length-1] = new Movie(title);  // it will set the last slot of the array

But you have a second problem with the Scanner. When you scan the input with nextInt(), the end-of-line is not read. It means that if you enter 2 (add a movie), the 2 is read, but not the new-line. Then the new-line is read by title = scan.nextLine() and you have an empty title... The solution is:

case 2:
   scan.nextLine();  // <-- add this to "eat" the previous new-line
   System.out.println("Please enter the movie you would like to add to the list:");
   title = scan.nextLine();
   movies = Arrays.copyOf(movies, movies.length+1);
   movies[movies.length-1] = new Movie(title);
   break;

Second question: check duplicate

Since you just have a plain/native array and no sorting between the movies, you can implement a simple for loop like:

case 2:
   scan.nextLine();  // <-- add this to "eat" the previous new-line
   System.out.println("Please enter the movie you would like to add to the list:");
   title = scan.nextLine();
   if (!checkDuplicate(title)) {
      movies = Arrays.copyOf(movies, movies.length+1);
      movies[movies.length-1] = new Movie(title);
   }
   break;

and add a function (assuming there is a Movie#getTitle():

    private static boolean checkDuplicate(String title) {
        for (Movie m : movies) {
            if (title.equals(m.getTitle())) {
                return true;
            }
        }
        return false;
    }
0

To expand the array by 1:

Movie[] temp = new Movie[movies.length + 1];
System.arraycopy(movies, 0, temp, 0, movies.length);
movies = temp;

To check if there is a duplicate movie object, implement equals in your Movie class. Then iterate through the existing movies, and check if movies[i].equals(newMovie).

mk.
  • 11,360
  • 6
  • 40
  • 54
  • For some reason, on my `case 2: ` it skips my .nextLine() and won't let me input a new movie. How can I fix this? –  Mar 15 '15 at 19:09
  • @Jordan You should ask a new question, after accepting the answer that best answers this current question. Seeing that you accept answers in your previous questions encourages people to answer your future questions. – mk. Mar 15 '15 at 19:13
  • 1
    @Jordan [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/q/7056749) – Tom Mar 15 '15 at 19:32
0

To expand the array, you must recreate an array of desired size and copy the content of previous array to new one.

   static Movie[] movies = new Movie[5];

  // expanding
  Movie[] newMovieArray = new Movie[10];
  for(int i = 0; i < 5; i++){
       newMovieArray[i] = movies[i];
  }
  movies = newMovieArray;
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69