1

I have a csv.file with 80 lines and 4 columns that represents one specific Movie Store.

  • 1st column: idSale
  • 2nd column: Movie
  • 3rd column: Genre
  • 4td column: Director

The file has a header with the names above and the rest of lines are the content. There are 79 sales, 30 movies, 10 directors and 8 genres. In the file one movie as only one genre and one director.

The movies, the genres and the directors are repeated... only the id is a non-repeated value.

I've managed to put the csv file into an array2D by counting the number of lines and columns. The array 2D is called movieCinema.

I've already managed a way to store each column into an array with non-repatead values because one of the tasks was to print the movies, the directors and the genres.

I've 3 arrays with repeated values with the following names:

  • movies
  • directors
  • genres

The length of this arrays is equal to the number of lines in the array2D-1 (because I didn't store the header).

And I've 3 arrays with non-repeated values)

  • moviesUnique (length equals 30)
  • directors (length equals 10)
  • genres (length equals 8)

Now, the last task is to print the movies by genre of the director Christopher Nolan. Example:

Christopher Nolan

Genre: Action

  • Movie 1
  • Movie 5

Genre: Drama

  • Movie 2

Genre: Sy-fy

  • Movie 3
  • Movie 4

I can't do this. I'm able to print all the movies of Chistopher Nolan and all the genres of him as well, but I can't put his the movies by genre.

This is my function to search:

public static String[] search (String[][] matrix, String searchItem, int searchCol, int indexCol) {
    int arraySize = 0;
    int index = 0;
    String[] items;

    // Compares each line (skip the header) that is equals to the max value and increases arraySize
    for (int i=1; i<matrix.length; i++) {
        if (searchItem.equals(matrix[i][searchCol])){
            arraySize++;
        }
    }

    // Declare array
    items = new String[arraySize];

    // Store all lines that has correspondence to the searchItem
    for (int i=1; i<matrix.length; i++){
        if (searchItem.equals(matrix[i][searchCol])){
            items[index] = matrix[i][indexCol];
            index++;
        }
    }

    return items;
}

Then I have a function to store only the unique values of an array called uniqueValues, that is working perfectly.

/**
 * Function that removes the duplicates of an array and stores only the unique valus
 * @param array
 * @return
 */
public static String[] uniqueValues (String[] array) {
    boolean duplicate;
    int index=0;
    int arraySize = 0;

    for (int i=1; i<array.length; i++) {
        duplicate = false;
        for (int j=i+1; j<array.length; j++) {
            if (array[i].equals(array[j]) && duplicate == false) {
                duplicate = true;
            }
        }

        if (duplicate == false) {
            arraySize++;
        }
    }

    String[] arrayUnique = new String[arraySize];

    for (int i=1; i<array.length; i++) {
        duplicate = false;
        for (int j=i+1; j<array.length; j++) {
            if (array[i].equals(array[j]) && duplicate == false) {
                duplicate = true;
            }
        }

        if (duplicate == false) {
            arrayUnique[index] = array[i];
            index++;
        }
    }

    return arrayUnique;
}

So in the main I call the functions:

String director = "Chistopher Nolan"

// This has movies and genres repeated
String[] directorGenresRepeated = search(movieCinema, director, 4, 3);
String[] diretorMoviesRepeated = Library.search(store, director, 4, 2);

// This has movies and genres non-repeated
String[] directorGenres = Library.uniqueValues(directorGenresRepeated);
String[] directorMovies = Library.uniqueValues(directorMoviesRepeated);

So if I print this array with a for I get all the movies of Chistopher Nolan and I get all the genres movies of Chistopher Nolan... It print this:

  • Genre 1
  • Genre 2
  • Genre 3

And then the movies:

  1. Movie 1
  2. Movie 2
  3. Movie 3
  4. Movie 4
  5. Movie 5

But the point is to have:

Genre: Action

  • Movie 1
  • Movie 5

Genre: Drama

  • Movie 2

Genre: Sy-fy

  • Movie 3
  • Movie 4

How can I do this?? I can't use lists or sets... or hashmaps... I can only use arrays, 2 arrays, for loops, while loops..

Could you help me?

DR8
  • 127
  • 5
  • So, do you need to read the CSV and list the movies categorized by actor and subcategorized by genre? – Diego Borba Jul 28 '23 at 17:17
  • @DiegoBorba "actor" doesn't even appear anywhere in the question. – David Conrad Jul 28 '23 at 18:51
  • @DavidConrad I mean **Director**, sorry. – Diego Borba Jul 28 '23 at 18:53
  • I suppose you will have to loop over the data and count how many genres there are for Nolan, then create an array to store counts, then loop again and find how many movies of each genre there are for Nolan, then create a 2D array for movies (I assume you mean titles) by genre, and then loop one last time to populate it. Or, if you don't need to store it, you could just output the results once you know all the genres for Nolan. – David Conrad Jul 28 '23 at 18:55
  • @DiegoBorba yes, thats what I want. I've managed to find a solution! – DR8 Jul 28 '23 at 22:27
  • @DavidConrad, thank you. I've managed to find a solution. I didn't create another 2D array, I just used a void function! But your solution to create an 2D array for movies (yes, the movies are the titles) by genre seems a lot better. – DR8 Jul 28 '23 at 22:28

2 Answers2

3
  1. Find all the unique genres associated with the director "Christopher Nolan" and store them in an array directorGenres.
  2. For each genre in the directorGenres array, find and store all the movies associated with that genre and director in a separate array moviesByGenre. 3.Print the movies for each genre along with the genre heading.

for reference :

    public static void printMoviesByGenreForDirector(String[][] movieCinema, String director) {
    // Step 1: Find unique genres for the given director
    String[] directorGenres = Library.uniqueValues(search(movieCinema, director, 4, 3));
    String[] directorMovies = Library.uniqueValues(search(movieCinema, director, 4, 2));

    // Step 3: Print movies for each genre
    for (String genre : directorGenres) {
        System.out.println("Genre: " + genre);
        boolean foundMoviesInGenre = false;
        for (String movie : directorMovies) {
            int movieIndex = -1;
            for (int i = 0; i < movieCinema.length; i++) {
                if (director.equals(movieCinema[i][4]) && genre.equals(movieCinema[i][3])) {
                    movieIndex = i;
                    break;
                }
            }
            if (movieIndex != -1) {
                System.out.println(movieCinema[movieIndex][1]);
                foundMoviesInGenre = true;
            }
        }
        if (!foundMoviesInGenre) {
            System.out.println("No movies found in this genre for " + director);
        }
    }
}

You can call this function in your main method with the specific director's name, like this:

public static void main(String[] args) {
    String[][] movieCinema = // Your 2D array containing movie data
    String director = "Christopher Nolan";
    printMoviesByGenreForDirector(movieCinema, director);
}

This function will print the movies for each genre of the given director in the desired format. Good Luck !!!

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • Hello, First of all thank you because thanks to you I've managed to do the task! Your answer isn't completely correct because It only prints an output with the first movie of each category and that movie was repeating and the categories and so were the genres... but the logic of your code allowed me to correct it. – DR8 Jul 28 '23 at 22:21
0

Thanks to @Zanzmera Jahanvi code I was able to find a code that puts the Movies by Genre of a specific director.

                public static void printMoviesByGenreForDirector(String[][] movieCinema, String director) {

                    // Step 1: Find unique genres for the given director
                    String[] directorGenres = Library.uniqueValues(search(movieCinema, director, 4, 3));
                    String[] directorMovies = Library.uniqueValues(search(movieCinema, director, 4, 2));


                    // Step 2: Print games for each category
                    for (int genreIndex = 0; genreIndex < directorGenres.length; genreIndex++) {
                        String genre = directorGenres[genreIndex];
                        System.out.println("\nGenre: " + genre);
                        boolean foundMoviesInGenre = false;
                        for (int i = 0; i < movieCinema.length; i++) {
                            if (director.equals(movieCinema[i][4]) && genre.equals(movieCinema[i][3])) {
                                System.out.println(movieCinema[i][2]);
                                foundMoviesInGenre = true;
                            }
                        }
                        if (foundMoviesInGenre == false) {
                            System.out.println("No games found in this genre for " + director);
                        }
                    }
                }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
DR8
  • 127
  • 5