Rafa_Asp1
Rafa_Asp1

Reputation: 89

Multidimensional array OutOfBondsException in Java

I having some problems with this code. I need to: Ask for name AND the number of movies that the customer rented in the last month. One array for the names, one bidimensional array for the quantity of movies rented in the last month in the first column and the number of free rentals the customer got (after two rents, the client gets one free) in the second column. Show the customer with the largest number of free rentals and what this number is.

import java.util.Scanner;
public class RentalStore
{
    public static final int NAMES = 10; //ten clients
    public static final int N_MOVIES = 10; //column with movies per client
    public static final int F_MOVIES = 2; //is granted free movie rentals.

    public static void ClientRegistration(String[] names, int[][] movies) {
        Scanner sc = new Scanner (System.in);
        for(int i=0; i< NAMES; i++) {
            System.out.println("Type the name of the client " + i + " and the amount of movies: ");
            names[i] = sc.next();
            movies[i][0] = sc.nextInt();
        }

        for(int i=0; i<F_MOVIES; i++) {
            movies[i][i] = movies[i][0]/2;
        }
    }

    public static void main (String [] args) {
        String[] names = new String[NAMES];
        int[][] movies = new int[N_MOVIES][F_MOVIES];
        Scanner sc = new Scanner (System.in);

        ClientRegistration(names,movies);

        int largest = 0;
        for(int i=0; i<F_MOVIES; i++) {
            if(movies[i][i] > largest) {
                largest = movies[i][i];
            }

        }
        for(int i=0; i<F_MOVIES; i++) {
            if(movies[i][i] == largest) {
                System.out.println("The client " + names[i] + " is entitled to " + largest + " movies.");
            }
        }
    }
}

The second column will store the number of free rents a customer have. But I get ArrayIndexOutOfBoundsException if I change the F_MOVIES to N_MOVIES.

Upvotes: 0

Views: 93

Answers (3)

Anuparna
Anuparna

Reputation: 98

The first thing I think you should change is :
for(int i=0; i<F_MOVIES; i++) { movies[i][i] = movies[i][0]/2; } to for(int i=0; i<F_MOVIES; i++) { movies[i][1] = movies[i][0]/2; } Basically, the places wherever you have movies[i][i] should be replaced with movies[i][1].

Another thing I guess you should look into is the calculation of the number of free rentals which as per your current code simply divides the number of movies by 2. But, as per your requirement, it should be 1 for every 2 rents. So, if the number of movies rented is 2, the free rent comes out to be 1. But, using your current code, if the number of movies is 3, the free rent becomes 0! So, the calculation of free rents should use N_MOVIES instead of F_MOVIES

Upvotes: 0

Hampton Young
Hampton Young

Reputation: 21

Your issue lies in

    for(int i=0; i<F_MOVIES; i++) {
        movies[i][i] = movies[i][0]/2; //change to movies[i][1]
    }

When iterating over N_MOVIES you will go out of bound since movies is defined as [N_MOVIES][F_MOVIES].

Upvotes: 0

YDF
YDF

Reputation: 21

It's easiest to think of a multidimensional array as a grid. You have:

                    F_MOVIES
  N_MOVIES         0       1  
    0              []      []
    1              []      []
    2              []      []
    3              []      []
    4              []      []
    5              []      []
    6              []      []
    7              []      []
    8              []      []
    9              []      []

Setting F_Movies equal to N_MOVIES would go out of bounds since F_MOVIES cannot exceed an index of 1

Upvotes: 2

Related Questions