I am trying to build a terminal mode application that allows a user to input certain attributes as a filtered search of Toyota vehicles released in the 80s. This is an assignment for a class so there are restrictions like having the original data in a CSV file and the data must be loaded into an ArrayList of objects (Toyota vehicles in my case). I decided to use a BufferedReader to read the data and set the values from the CSV file as arguments for instantiating new vehicles and adding them to the ArrayList.
Here is the method implementing what I just explained:
public void load() {
toyotaVehicleCatalog = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader("./src/files/80stoyota.csv"))) {
String line = "";
while((line = br.readLine()) != null) {
String[] fields = line.split(",");
int modelYear = Integer.parseInt(fields[0]); //Line 28
String modelName = fields[1];
String modelCode = fields[2];
String modelEngine = fields[3];
boolean sedan = fields[4].length() > 0 ? true : false;
String funFact = fields[5];
ToyotaVehicle vehicle = new ToyotaVehicle(modelYear, modelName, modelCode, modelEngine, sedan, funFact);
toyotaVehicleCatalog.add(vehicle);
}
} catch(IOException ex) {
ex.printStackTrace();
}
for(int i = 0; i < toyotaVehicleCatalog.size(); i++) {
System.out.println(toyotaVehicleCatalog.get(i));
}
}
My ToyotaVehicle class:
public class ToyotaVehicle {
private String modelCode;
private int modelYear;
private String modelName;
private String engine;
private boolean sedan;
private String funFact;
public ToyotaVehicle(int modelYear, String modelName, String modelCode, String engine, boolean sedan, String funFact) {
this.modelCode = modelCode;
this.modelYear = modelYear;
this.modelName = modelName;
this.engine = engine;
this.sedan = sedan;
this.funFact = funFact;
}
public ToyotaVehicle() {
}
public String getModelCode() {
return modelCode;
}
public void setModelCode(String modelCode) {
this.modelCode = modelCode;
}
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public boolean isSedan() {
return sedan;
}
public void setSedan(boolean sedan) {
this.sedan = sedan;
}
public String getFunFact() {
return funFact;
}
public void setFunFact(String funFact) {
this.funFact = funFact;
}
}
When I run the code, I get this error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at data.LoadData.load(LoadData.java:28) at Driver.main(Driver.java:7)
I apologize in advance if I seem to be missing an obvious mistake here. The only thing I've tried is at first I used the files[] array values as direct arguments rather than setting variables for each array value, no luck. I've read up on most forums/posts relating to this same issue and have yet to find a solution. I've used BufferedReader a handful of times before and have never ran into this problem. Any help would be appreciated, thanks.
There's a blank line in the input file. Possibly at the end. This results in the result of
.split(",")to be a zero-length array, thus resulting in anArrayIndexOutOfBoundsExceptionwhen tryingfields[0]. Trivially by checking for this in your loop: