1

I really need some help with my assignment in school. It's been only a month since I'm in Java and I got this super hard assignment to accomplish. I'm struggling for several days now. So I have to make a car shop database with 20 cars. I managed to make a Car class with getters and setters. Now I have to make a bunch of operations that the car shop owner can do with his info of cars. I have info about 3 cars (carsArray [0] [1] [2]) and other 17 empty. The main issue is how to add more info to the next free array (with scanner).

    Car carsArray[] = new Car[20];   //cars array with 20 Car objects
    for (int c = 0; c < carArray.length; c++)
    carArray[c] = new Car();

    carArray[0].setRegNr("A123");
    carArray[0].setModel("v50");
    carArray[0].setYear(2005);
    carArray[0].setMileage(2000);
    carArray[0].setGear("manu");
    carArray[0].setColour("white");
    carArray[0].setFuel("diesel");

    // this is what I'm trying now with methods (sorry, not sure att all how to use my                 
    // arrays)
    public static String[] addCar() {

    Car[] c = new Car;
    for (int i = 0; i < c.length; i++) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter reg nr.: ");
    carsArray[].setRegNr() = keyboard.nextLine();
    {
    return ???;
    }

    
4
  • Well, you don't have free array positions, as you are making 20 Car objects within the for loop. Commented Jan 14, 2021 at 10:33
  • Hint: always use braces {} with for loops and if statements, even if they are not strictly necessary. Commented Jan 14, 2021 at 10:34
  • I meant free from descriptions. Thanks for advice! :) Commented Jan 14, 2021 at 12:33
  • Nevertheless, I recommend you to write a constructor for the Car class, which accepts several parameters. At the moment, one is able to create a car (using new Car()) without a model, without a build year and without a fuel type. Commented Jan 14, 2021 at 12:39

3 Answers 3

2

you can use Arraylist

      ArrayList<Car> cars = new ArrayList<Car>();
car inputCar =new Car();
inputCar.setFuel(keyboard.nextline());
inputCar.setModel(keyboard.nextline());
// and get all infos you need then
cars.add(inputCar);

`

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Shahab, but I'm not allowed to use ArrayList (to make everything more complicated I guess) :(
well no problem you are using a for loop in your code you can get every thing you need from user input then you can use carsArray[i].setcolour(keyboard.nextline()) @BaibaDzilna
OK, will try :)
Upvote because the answer is good @Shahab Hosseini
0

Try this

public static void main(String[] args) {
        Car carsArray [] = new Car[2]; /* Array has 2 cars 
        for example */
        
        for(int c = 0; c < carsArray.length; c++){
            Car car = addCar(); /* assign the car you're trying 
            to add to a variable of type car
            */
            carsArray[c] = car; /*add that car in the array 
            of cars*/
        }
        
        for(Car car : carsArray){ /*for a car in the array of cars*/
            System.out.println("Reg No : " + car.getRegNr());
            System.out.println("Model : " + car.getModel());
            System.out.println("Year : " + car.getYear());
            /* similarly disply all info related to that car*/
            System.out.println();
        }
          
    }
    
    static Car addCar(){ //This method returns a Car object
        Car car = new Car();
        Scanner scanner = new Scanner(System.in); 
        System.out.print("Enter registration number : ");
        car.setRegNr(scanner.next()); //next() for strings
        System.out.print("Enter model : ");
        car.setModel(scanner.next());
        System.out.print("Enter year : ");
        car.setYear(scanner.nextInt()); //nextInt() for int
        /*
          Continue by :
          set mileage,gear,color,fuel similiarly 
        */
        return car; // return this Car, which you're going to add
    }

1 Comment

Please don't forget to thank me, by accepting this answer as correct :-)
0

Shahab's answer is probably this best way to do it as the number of cars is not important and you can store as many cars as you need (An java.util.ArrayList grows as you add more entries). However if you must use an array whenever you add a new entry (and you don't know which array indices are available i.e. you don't know what it contains) you will need to check in the array where the next empty index is like so:

for(int i = 0; i < carArray.length; i++){
   if( carArray[i] != null && carArray[i].getRegNr() != null){
        carArray[i] = new Car();
        carArray[i].setRegNr(keyboard.nextline());
        carArray[i].setModel(keyboard.nextline());
        //etc...
        break;
   }
}

But like I said earlier from a practicality stand point this is probably not worth it as you have to manage the array size manuallly and the array will eventually run out of space, however it really depends on the use case.

1 Comment

Thanks! I have to learn to use and to understand all the good tips :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.