'Returning null when expected to return int value

I have an assignment:

  1. I need to create 3 classes that are using each other
  2. Already created the first two and now I'm in the third and final
  3. I'm being requested to loop through an array of objects, get the string attribute of each one, and if it matched to the given parameter then return the first object number matched by its order, if I have another object matched to the given parameter then I need to return the index position of the first occurrences between them if nothing is matched to the given parameter then I need to return null

The referenced method is getHall(String Movie)

public class CinemaCity {   // 3rd and final class


private final int NUM_OF_HALLS = 15;
private Hall[] _halls; // Hall Object is the 2nd class is the 2nd class 

public CinemaCity() // Constructor to intialize calss attribute
{
    _halls = new Hall[NUM_OF_HALLS]; 
}

public int getHall(String movie) // should I return String or int? how to achieve it
{

}

Basically, if i have 2 halls with the same movie n I need to return the position of the first hall object in the array matched to the given parameter even if I have 3 halls I need to return the first hall number presenting the movie. and if nothing matches the given parameter then I need to return null

Here I'm getting stuck with keeping first occurrence in array list and returning it

i thought of doing this

public int getHall(String movie) {
    int firstCatch = 0;
    int hallNumber = 0;

    for (int i = 0; i < _halls.length; i++) {
        if (_halls[i].getMovieName().equals(movie)) { //check the object movie name is equals to given parameter
            return i + 1; //if matched then just return the first hall object without going all over the other elemtns and see there are another match
        }
    }
    return null; // obviously I'm getting an error since it's expecting an int
}


Solution 1:[1]

guys, I'm back again,

After seeking deeper clarification, it seems that I need to return the object -> returning the "HALL" object the Method signature is:

public Hall getHall(String movie)

As I tried to do something based on returning a string due to my previous understanding of the assignment I return the index number of Hall, which was the first occurrence in the array.

 public String getHall(String movie) {

    int counter = 0;
    int[] _newHall;
    _halls = new Hall[NUM_OF_HALLS];
    for (int i = 0; i < _halls.length; i++)
        if (_halls[i].getMovieName().equals(movie))
            counter++;

    _newHall = new int[counter];
    for (int i = 0; i < _halls.length; i++)
        if (_halls[i].getMovieName().equals(movie))
            _newHall[i] = i+1;

    if(_newHall.length == 0)
        return null;
    else
        return ""+_newHall[0]+"";
}

Now that I need to return an Object, it's getting me confusing, and I'm trying to manipulate it over to get an object, but it looks like my brain does not function.

Repeating the assignment request again
I already have two classes, one called Timer (Object for the time of movie) second class Hall (the Hall which the movie represents and other attributes)

In the current Class cinemaCity getHall(String movie) Method need to return the first occurrence object, which has the movieName matched to the parameter This means if there are other objects with the same movie name, then I need to return the first object(occurrence) to match

I hope you can help me; also, if you are answering, if possible to get an explanation of the logic behind it for better understanding.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KakakshiD0m0