1

In my task I need to store class Car into setOfCars. Reading must be done from file. I have id, nameOfCar, noOfSeats.

Input:

1
Mercedes
GTR 2

In main I have this:

Set<Car> listOfCars= eneterCars();

But intellij cast it to:

 Set<Cars> listOfCars= (Set<Car>) eneterCars();

And this doesnt work.

Method I call is also in main:

  File cars= new File("dat/cars.txt");

try(FileReader fileReader = new FileReader(cars);
    BufferedReader reader = new BufferedReader(fileReader)){
    String readLine;
    List<Car> listOfCars= new ArrayList<>();
    while((readLine= reader.readLine()) != null){
        Long id = Long.parseLong(readLine);
        String name= reader.readLine();
        Integer noOfSeats=Integer.parseInt(reader.readLine());
        listOfCars.add(new Car(id, name, noOfSeats));
        //System.out.println(id + " name" + name+ " " + noOfSeats);
    }

return cars;

I want to store information from file to set so that I could use it later. But I get that cast error when I call this function.

1
  • return cars actually returns the file not the list. Commented Dec 1, 2020 at 19:50

2 Answers 2

2

You have written

return cars;

which is File cars= new File("dat/cars.txt");. You need to return the correct object as

return listOfCars;

Also, instead of declaring List<Car> listOfCars= new ArrayList<>(); in the method, you could have declared it as Set<Car> listOfCars= new LinkedHashSet<>(); if you needed a Set instead of a List.

If you still want to keep the declaration as it is, an idiomatic way to convert it to Set is as follows:

Set<Cars> listOfCars= new LinkedHashSet<>(eneterCars());

Note: LinkedHashSet retains the order to insertion. If the order to insertion does not matter for you, you can use HashSet.

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

2 Comments

Thanks it helped !
Thanks I did that
1
public final class Car {

    private final int id;
    private final String manufacture;
    private final String model;

    public Car(int id, String manufacture, String model) {
        this.id = id;
        this.manufacture = manufacture;
        this.model = model;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!(obj instanceof Car))
            return false;
        return id == ((Car)obj).id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

public static Set<Car> readCarsFromFile(File file) throws IOException {
    try (Scanner scan = new Scanner(file)) {
        Set<Car> cars = new LinkedHashSet<>();

        while (scan.hasNext()) {
            int id = scan.nextInt();
            scan.nextLine();
            String manufacture = scan.nextLine();
            String model = scan.nextLine();
            cars.add(new Car(id, manufacture, model));
        }

        return cars;
    }
}

cars.txt

1
Mercedes
GTR 2
2
BMW
X5

Demo:

Set<Car> cars = readCarsFromFile(new File("dat/cars.txt"));
// 2 cars: Mersedes, BMW

Comments

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.