开发者

Java comparing two strings doesnt seem to work

I got this function which is a private boolean function that checks if there is a car already of the same size inside the garage. If there isn't I add it to the arrayList, I've made a printList() type function before to traverse through the arraylist and print out the values (which works perfectly) but somehow my private boolean function doesnt seem to work at all.

Heres my code:

public class Cars {

    public Cars (String size, boolean booking) {
        this.carSize = size;
        this.isBooked = booking;
    }

    public String getSize() {
        return this.carSize;
    }

    public boolean checkBook () {
        return this.isBooked;
    }

    private String carSize;
    private boolean isBooked;

}

public class Locations {

    public Locations (String curLocation) {
        garage = new ArrayList<Cars>();
        location = curLocation;
    }

    public void addCar (String size, boolean booking) {
        if (garage.isEmpty() || !checkCar(size)) {
            garage.add(new Cars(size, booking));
            System.out.println("Car assigned " + location + " " + size);
        }
    }

    private boolean checkCar (String size) {
        for (Cars car : garage) {
            System.out.println("hey");
            if (size.equals(car.getSize())) return true;
        }
        return false; 
    }

    private ArrayList <Cars> garage;
    private String location;

}

The input is as the following:

Car small City
Car small Redfern
Car small Redfern

output:

Car assigned City small
Car assigned Redfern small
Car assigned Redfern small

it should never print out the second Redfern small, as 开发者_如何学Cthere already is that size car inside the list.


(My previous answer was wrong - I misread the code in my haste ...)

I can think of only one explanation for what is going on:

You have called new Locations("Redfern") twice.

This would explain why you are seeing the message Car assigned Redfern small twice, and why you are NOT seeing hey.

You can confirm this by putting a traceprint in the Locations constructor ...


The theory that this is caused by leading / trailing whitespaces on one of the size strings doesn't hold water. If that was the problem, the OP would see hey as the checkCar method iterated the garage list.


If I use your code in the following way:

public class Main
{
    public static void main( String[] args )
    {
        Locations locationsCity = new Locations( "City" );
        locationsCity.addCar( "small", true );
        Locations locationsRedfern = new Locations( "Redfern" );
        locationsRedfern.addCar( "small", true );
        locationsRedfern.addCar( "small", true );
    }
}

this is the output I am getting:

Car assigned City small
Car assigned Redfern small
hey

which seems just fine and according to your code.


Consider putting your cars in a set and implement equals() to just check the size using the String.equals() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜