In Java, how do I use a loop to instantiate new objects, store them in an array, and then use them?

16,049

My problem is how do I create a new Car object each time I go through the loop and read in new data that doesn't have the same name?

Objects don't have names.

As is, Ill just I'll just be making a bunch of objects all named car1.

No, you'll be creating a lot of objects, that's all. In each iteration, you'll assign a reference to a variable called car1, but a variable is not an object - and even the value of the variable isn't an object, it's a reference.

It would be simpler to get rid of car1 entirely though, and just assign the reference directly into the array:

carArr[i] = new Car(make, year, price);

It would be even better to use a List<Car> instead of an array - then you don't need to keep track of the size separately. I'd write the code as:

Scanner scan = new Scanner(System.in);
List<Car> cars = new ArrayList<Car>();

while (!scan.next().equals("EndDatabase"))
{
   String make = scan.next();
   int year = scan.nextInt();
   double price = scan.nextDouble();
   cars.add(new Car(make, year, price));
}

For the sake of the assignment you'll probably need to use an array, but that's just annoying...

I'd also probably change the price to be a BigDecimal instead of double - you shouldn't use a binary floating point type for currency values.

(Do you definitely want to discard the first value read in each iteration of the loop, by the way? All you're doing is checking whether it's EndDatabase - if it's not that, you're basically losing the information of whatever it was...)

Share:
16,049
art3m1sm00n
Author by

art3m1sm00n

Updated on June 05, 2022

Comments

  • art3m1sm00n
    art3m1sm00n almost 2 years

    I have an introductory assignment for my CS class. I have a class named Car and a separate data file for later file redirection. I am to:

    use a sentinel loop to read in the make, year, and price of each Car object, instantiate the object, and store it in the array carArr[]. For example, if you create a car object assigned to a variable named car1, you can just do: carArr[i] = car1 where "i" is an int counter. Also, count the elements of the array as they are stored in an int variable like "i" or numCars

    Here is my code so far:

    Scanner scan = new Scanner(System.in);
    final int SIZE_ARR = 30;
    Car [] carArr = new Car[SIZE_ARR];
    int i = 0;
    int numcars = 0;
    String make = "";
    int year = 0;
    double price = 0.0;
    final String SENT = "EndDatabase";
    
    while (!scan.next().equals(SENT))
    {
       make = scan.next();
       year = scan.nextInt();
       price = scan.nextDouble();
       Car car1 = new Car(make, year, price);
       carArr[i] = car1;
       i++;
       numCars++;
    }
    

    How do I create a new Car object each time I go through the loop and read in new data that doesn't have the same name? Do they need to have different names? As is, I'll just be making a bunch of objects all named car1. I will eventually have to print out the database and then read them in a new Car object as a search key. The key will then sequentially search through the carArr array. Will the search get confused because every element in the array is filled with an object of the same name?

    Also, when I go through to search the array and compare my stored objects to my search object do I just need to do:

    key.equals(carArr[i])
    

    Will that compare the instance variables of each object to each other?

    In my Car class, I am required to have an accessor method for my year, make, and price instance variables. Where would I need to use these? It also states I have to have a "mutator method called setPrice" and "an equals method". Any idea what they mean for me to do in those? Sorry for the length of this and the multiple questions. I am just trying to stay ahead of my workload and my teacher isn't answering emails. THANKS!

    **********EDIT********************************************** I figured it out. Thanks!