Margarita Kattsyna
Margarita Kattsyna

Reputation: 5

an ArrayList with for and while loops. It should be another order of statements

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List < Car > cars = new ArrayList < > ();
        while (scan.hasNext()) {
            Car car = new Car();
            String make = scan.nextLine();
            if ("-1".equals(make)) break;
            cars.add(car);
            String model = scan.nextLine();
            if ("-1".equals(model)) break;
            car.setMake(make);
            car.setModel(model);
        }
        scan.close();
        for (Car car: cars) {

            System.out.println(car.getMake() + " " + car.getModel());

            System.out.format("There are %s cars \n", cars.size());
        }
        System.out.format("%s", "\n");
    }
}
public class Car {
    private String make;
    private String model;

    public Car() {
        this.make = make;
        this.model = model;
    }

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

a program that populates an ArrayList with the make and model of cars.

program should output the makes and models of all cars in the ArrayList.

input:

Honda Fit Toyota Corolla Mazda Axela

Finally, your program should output the makes and models of all cars in the ArrayList. The program prints this to standard output:

There are 3 cars Honda Fit Toyota Corolla Mazda Axela

Where I have an error? It should be another order of statements.

Upvotes: 0

Views: 32

Answers (1)

Reilas
Reilas

Reputation: 6266

The initial error with your program is the use of the Scanner#hasNext method.

While your concept is correct, this is not the method you'll want to use when reading standard-in input.

Essentially, the Scanner will wait for more input, indefinitely.
Thus, it is not a way to determine if the user has completed entering values.

Change this to the following.

Car car;
String make, model;
do {
    car = new Car();
    car.setMake(make = scan.nextLine());
    if (make.isEmpty()) break;
    car.setModel(model = scan.nextLine());
    cars.add(car);
} while (!model.isEmpty());

Additionally, you need to change the subsequent for-loop variable name, car, since we are now using that name.

Output

Honda
Fit
Toyota
Corolla
Mazda
Axela

Honda Fit
There are 3 cars 
Toyota Corolla
There are 3 cars 
Mazda Axela
There are 3 cars 

Here are a few improvements you can make to your code.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    List<Car> cars = new ArrayList<>();
    String make, model;
    do {
        make = scan.nextLine();
        if (make.isEmpty()) break;
        model = scan.nextLine();
        cars.add(new Car(make, model));
    } while (!model.isEmpty());
    scan.close();
    for (Car car : cars) System.out.println(car);
    System.out.format("There are %s cars \n", cars.size());
    System.out.format("%s", "\n");
}
public class Car {
    private String make, model;

    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    public String toString() {
        return make + " " + model;
    }
}

Upvotes: 0

Related Questions