-1

So, I'm trying to have my app search through an array where each object contains multiple values, like so:

public static String name;
public Boolean inUse;
public static Boolean covered;
parkingSpots[] spots = new parkingSpots[9];

 spots[0] = new parkingSpots("A1", false, false);
 spots[1] = new parkingSpots("A2", false, false);
 spots[2] = new parkingSpots("A3", false, false);
 spots[3] = new parkingSpots("B1", false, false);
 spots[4] = new parkingSpots("B2", false, false);
 spots[5] = new parkingSpots("B3", false, false);
 spots[6] = new parkingSpots("C1", false, true);
 spots[7] = new parkingSpots("C2", false, true); 
 spots[8] = new parkingSpots("C3", false, true);

What I want it do to is search to see if the spot is both covered and not in use, and then return the name of the first available spot with that criteria, so I did this:

 public boolean coveredSearch() {
    if (Arrays.asList(spots).contains(covered = true)) {
        if (Arrays.asList(spots).contains(inUse = false)) {
            return Arrays.asList(spots).contains(name);

        }

    }

But for some reason, running this method crashes my app. Does anyone know the reason why?

TDG
  • 5,909
  • 3
  • 30
  • 51
Jumper
  • 65
  • 10

4 Answers4

2

Contains uses equals, internally. You're passing an assignment as parameter, guessing it would infer which property you want to compare in order to define equality, but that doesn't work this way. You question might be related to How does a ArrayList's contains() method evaluate objects?.

What defines equality in this case, is your class equals implementation.

Community
  • 1
  • 1
Leonardo Lana
  • 590
  • 1
  • 4
  • 13
1

Your code checks if there are any covered spots, and if there are any unused spots, but doesn't check if there is a spot that is both at the same time. You can't use independent contains() calls, even if you were using them right, which you are not (see other answers).

What you need is a simple search loop to find a spot that is both covered and unused, then return the name of that spot:

public String findCovered() {
    for (parkingSpots spot : this.spots)
        if (spot.isCovered() and ! spot.isInUse())
            return spot.getName();
   return null; // No unused covered parking spots available
}

Or, if using Java 8:

public Optional<String> findCovered() {
    return Arrays.stream(this.spots)
                 .filter(s -> s.isCovered() and ! s.isInUse())
                 .map(parkingSpots::getName)
                 .findAny();
}

Also, be aware that your fields are wrong. The name and covered fields must not be static, and the inUse and covered fields should be boolean, not Boolean.

Maybe it was just an abbreviation of the actual code, but spots should not be a field of parkingSpots. If it has to be there, it must be static.

public class parkingSpots { // Should be name ParkingSpots
    private String name;
    private boolean inUse;
    private boolean covered;
    // constructors and methods here
}
public class ParkingLot {
    private parkingSpots[] spots;
    // constructors and methods here, incl. findCovered()
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

As said you cannot use contains like that. Contains checks that an element is in a List using the equals() method.

Put this method in your ParkingSpots class. And call it to find your desired spot.

public String coveredSearch(List<ParkingSpots> parkingSpots) {
        for (ParkingSpots p: parkingSpots) {
            if (!p.inUse && p.covered) {
                return p.name;
            }
        }
        return "Cannot find parking space";
    }

Testing:

public class SpacesTest {

    public static void main(String[] args) {

        ParkingSpots ps = new ParkingSpots("Dummy", false, false);

        ParkingSpots[] spots = new ParkingSpots[9];

         spots[0] = new ParkingSpots("A1", false, false);
         spots[1] = new ParkingSpots("A2", false, false);
         spots[2] = new ParkingSpots("A3", false, false);
         spots[3] = new ParkingSpots("B1", false, false);
         spots[4] = new ParkingSpots("B2", false, false);
         spots[5] = new ParkingSpots("B3", false, false);
         spots[6] = new ParkingSpots("C1", false, true);
         spots[7] = new ParkingSpots("C2", false, true); 
         spots[8] = new ParkingSpots("C3", false, true);

         System.out.println(ps.coveredSearch(Arrays.asList(spots)));
    }    
}

Output: C3

Also as said I recommend you rename ParkingSpots to ParkingSpot.

Kusterbeck
  • 108
  • 8
0

I don't know what the error is but your code is not going to work.

You have the following

Arrays.asList(spots).contains(covered = true)

This is basically the same thing as the following code.

covered = true;
Arrays.asList(spots).contains(covered)

And because true will never equals any of your parkingSpots instances it will always return false.

Note on Naming

By convention your class should be called ParkingSpot not parkingSpots it makes your code easier to read.

cyroxis
  • 3,661
  • 22
  • 37
  • Thank you for the answer. If you don't mind, could you point me in the direction of something I could try? – Jumper May 27 '16 at 19:24