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 6913147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:10:53+00:00 2026-05-27T09:10:53+00:00

I have a method which adds a new vehicle(Honda) to the vehicles array. The

  • 0

I have a method which adds a new vehicle(Honda) to the vehicles array. The array contains a maximum of 4 vehicles.

Vehicle[] vehicles = new Vehicle[4];

The method is supposed to add 1 new Vehicle object to the end of the vehicles array if there is a null value. The problem is that it is writing to ALL of the null values in the array instead of just writing to 1 and then kicking out of the for loop.

Here is what I have (NOTE — I am required to use an array instead of ArrayList):

public void addVehicle(Vehicle Honda[]) throws FileNotFoundException
{
    boolean found = false;
    if(canAddVehicle() == true)
    {
        for(int i = 0; i < vehicles.length || !found; i++)
        {
            if(vehicles[i] == null)
            {
                Scanner reader = new Scanner(file);
                Honda[i] = new Vehicle();
                Honda[i].readRecord(reader);
                vehicles[i] = Honda[i];
                reader.close();
                found = true;
            }

        }
        System.out.println("Vehicle Added!");

    }
}

I’ve set found = true to make sure it leaves the for loop as soon as it finds the first null value in the array.. but it doesn’t seem to be working. Why would this be?

EDIT: Also, I am not allowed to have any other class level data.

  • 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-27T09:10:53+00:00Added an answer on May 27, 2026 at 9:10 am

    You’re using || when you should be using &&:

    for(int i = 0; i < vehicles.length && !found; i++)
    

    More information on the conditional operators can be found in this Java Tutorials article.

    As a friendly critique, this isn’t very readable to another developer. The following would be easier to follow:

    for(int i = 0; i < vehicles.length; i++)
    {
        if(vehicles[i] == null)
        {
            Scanner reader = new Scanner(file);
            Honda[i] = new Vehicle();
            Honda[i].readRecord(reader);
            vehicles[i] = Honda[i];
            reader.close();
            break; //break out of the loop
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method which adds a new column to a Telerik RadGridView:
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method which constructs an object, calls an Execute method, and frees
I have a method which never returns a null object. I want to make
I have a method which adds investment status by storedprocedure (in this method I
I have a simple ajax method in my MVC.Net project which adds entries to
I have a Post, to which a migration adds a new attribute and table
I have a method which should be executed in an exclusive fashion. Basically, it's
I have an method which save files to the internet, it works but just
I have a method which is given the parameter bool sortAscending. Now I want

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.