1

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?

2
  • cars[index].getNumber() if you have a getter method. Commented Dec 9, 2015 at 23:41
  • what is 'cars' type. Is it its own class? Then you either have a getter method mentioned previously or a public variable you can access. Please post the code for how you set the value of each carNumber and we can help probably. Commented Dec 9, 2015 at 23:44

4 Answers 4

3

You had many errors in such a small code.

  1. while (!found) this means that you will continue searching until you found something, but what happens if the element we're searching is not on the array ? We will get the infamous ArrayOutOfBoundsException due the fact we will try to access non-existing array cell. Solution: for (int i = 0; i < cars.length; i++) in order to run from the start until the size of the array.
  2. Found, now what ? you didn't stopped when finding the car you were looking for, now we're setting it as found = true and stopping the search.

Code:

public int findBayOfCar(int carNumber)
{
    int foundAtBay = -1;
    for (int i = 0; i < cars.length; i++)
    {
        if (cars[i].number == carNumber) // or whatever .Number or .number which identify the car number.
        {
            foundAtBay = i;
            break;
        }
    }
    return foundAtBay;
}
Sign up to request clarification or add additional context in comments.

1 Comment

You don't want to return a boolean found, you want to return the bay number in which the car was found.
1

You should of reference your Car object to obtain its number:

public static int findBayOfCar(int carNumber, Car[] cars)
{
  int index = -1;
  boolean found = false;

  while (!found && index < cars.length -1)
  {
    index++;
    if (cars[index].getNumber() == carNumber)
    {
       found = true;
    }  
  }
  return found ? index : -1;
}

Comments

1

A simple and a brute-force solution to it would be LinearSearch:

for(int i=0; i < your_array_length; i++){
    if(cars[i] == carNumber){
      return i;
    }
}
return -1; //-1 represents that the car was not parked in any of the slots

1 Comment

Omit the break; after the return as it's unreachable.
0

If your cars are just numbers, then:

int[] cars = new int[] { 1, 2, 3 } ;
public int indexOf(int carNumber) {
    return Arrays.binarySearch(cars, carNumber);
}

If your cars are strings, then

String[] cars = new String[] { "car1", "car2", "car2" };
public int indexOf(int carNumber) {
    return Arrays.asList(cars).indexOf("car" + carNumber);
}

If your cars are objects, then

Car[] cars = new Car[] { new Car(), new Car(), new Car()  };
public Car indexOf(int carNumber) {
     return IntStream.range(0, cars.length).filter(c -> carNumber == cars[c].number).mapToObj(i -> cars[i]).findFirst().orElse(null);
}

Or, with an ordinary loop:

Car[] cars = new Car[] { new Car(), new Car(), new Car() };
public int indexOf(int carNumber) {
    for (int i = 0; i < cars.length; i++) {
        if (cars[i].number == carNumber) {
            return i;
        }
    }
    return -1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.