In my Java program, I am simulating a parking garage. Suppose there are a random number of cars, let's say 7. Car number 1 is referred to by the value 1, car 2 is referred to by the value 2, and so on. Each car value is randomly stored in an array that represents the parking garage. Each element of the array represents a parking spot. I am trying to write a method that does the following: Suppose you want to know what parking space car number 4 is in. The value "4" is passed to the method, and the method will search the array for that value. I want the method to tell me which element of the array the value "4" was found in. Here is what I have so far for the method:
public int findBayOfCar(int carNumber)
{
int index = -1;
boolean found = false;
while (!found)
{
if (cars[index] == carNumber)
{
}
index++;
}
}
Obviously it will not work because Java cannot compare cars[index] (which is an array of the Car type) to carNumber, which is an int. What can I do to correct this?
cars[index].getNumber()if you have a getter method.