Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6338283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:24:47+00:00 2026-05-24T19:24:47+00:00

I got this function which is a private boolean function that checks if there

  • 0

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 there already is that size car inside the list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:24:48+00:00Added an answer on May 24, 2026 at 7:24 pm

    (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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
I've got this simple function which displays a message to the user. If I
I've got a .js file that has this function in it: function showDialog(divID) {
In the end, I got this function. I don't know whether it's normal or
I got this from for a login form tutorial: function sanitize($securitystring) { $securitystring =
I've got a simple function like this: function CreateFileLink() { global $Username; return date(d-m-y-g-i);
I've got this script on my website : $(document).ready(function() { function filterPath(string) { return
So i got this snippet of code: $(document).ready(function () { var replacementDoneIn = $(body).html();
I've got this bit of JQUERY: <script> function initNav() { $('.nav tr table td
I've got a JavaScript object, built this way: function foo() { this.length = 0;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.