Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Confused on making an array of a class

So I have this code already pre-written for me

public class Car
{
    private String make;
    private int yearModel;
    private double price;
    private int mileage;

    public Car (String mk, int year, double pr, int miles)
    {
        make = mk;
        yearModel = year;
        price = pr;
        mileage = miles;
    }

    public String getMake()
    {
        return make;
    }

    public int getYearModel()
    {
        return yearModel;
    }

    public double getPrice()
    {
        return price;
    }

    public int getMileage()
    {
        return mileage;
    }

    public String toString()
    {
        return "Make: " + make+"\n"+"YearModel: " + yearModel +"\n"+
        "Price: $" + price +"\n" + "Mileage: "+ mileage + " miles\n\n";
    }

}

My task is to first prompt the user for the number of cars on a car lot. Then create an array of Car to hold the input number of cars. Then fill the array with the info of every car on the carlot. I know the general concept, but when I’m trying to input a String, it says a type mismatch with the array Cars only allowing for the type Car. I’m confused what that is and how to get around it. Here’s my code.

import java.util.Scanner;

public class CarLot {
    public static void main (String [] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Input number of cars: ");
        Car [] Cars = new Car [input.nextInt()];
        for(int i = 0; i < Cars.length; i++) {
            System.out.println("Make");
            Cars[i] = input.nextLine();
            System.out.println("Model");
            Cars[i] = input.nextLine();
            System.out.println("Price");
            Cars[i] = input.nextLine();
            System.out.println("Mileage");
            Cars[i] = input.nextLine();
            System.out.println();
        }
        input.close();
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s important to note that Scanner .nextLine() does not return a Car, it returns a String. You need to make a Car out of the inputs.

So in the loop you should do something like this (I made the cars variable name lowercase to follow Java naming conventions):

System.out.println("Make");
String make = input.nextLine();
System.out.println("Model");
String model = input.nextLine();
System.out.println("Price");
double price = input.nextDouble();
System.out.println("Mileage");
int mileage = input.nextInt();

cars[i] = new Car(make, model, price, mileage);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading