Quantcast
Channel: Active questions tagged filereader - Stack Overflow
Viewing all articles
Browse latest Browse all 107

In the java code below which is a method of a larger program my ArrayList is returning empty but the objects being added can be printed

$
0
0
public static ArrayList<Vehicle> loadVehicles(String filePath) {    ArrayList<Vehicle> vehicles = new ArrayList<>();    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {        String line;        while ((line = reader.readLine()) != null) {            if (line.startsWith("[Vehicle]")) {                String makeModel = null;                String quality = null;                int seats = 0;                int rentalPrice = 0;                boolean available = false;                // Read each attribute of the vehicle                while ((line = reader.readLine()) != null && !line.isEmpty()) {                    String[] parts = line.split(": ");                    switch (parts[0]) {                        case "MakeModel":                            makeModel = parts[1];                            break;                        case "Quality":                            quality = parts[1];                            break;                        case "Seats":                            seats = Integer.parseInt(parts[1]);                            break;                        case "RentalPrice":                            rentalPrice = Integer.parseInt(parts[1]);                            break;                        case "Available":                            available = Boolean.parseBoolean(parts[1]);                            break;                        default:                            break;                    }                }                // Create and add the vehicle to the list                Vehicle v = new Vehicle(makeModel, quality, seats,                                     rentalPrice, available);                vehicles.add(v);                System.out.println(vehicles);                System.out.println(v);            }        }    } catch (IOException e) {        e.printStackTrace();    }    return vehicles;}

I tried printing out the ArrayList and nothing is printed I checked the size and its 0I have ensure that the file is indeed being read and data retrieved but I dont understand why the arrayList size is 0


Viewing all articles
Browse latest Browse all 107

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>