Reputation: 33
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 ???;
}
Upvotes: 1
Views: 409
Reputation: 143
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.
Upvotes: 0
Reputation: 357
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
}
Upvotes: 0
Reputation: 31
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);
`
Upvotes: 2