I'm not sure how to read from a CSV into an arraylist in java.
I have an arraylist of items and in my method I have instantiated ArrayList<Item> itemz = new ArrayList<Item>();
I have to fill itemz with items from the CSV which have String, Int, Int values in them. Then return itemz;
Is this correct? I believe all the logic is correct but I can't compile/test it due to not even being near done with the other classes in the assignment.
Code
public ArrayList<Item> getGameItems(String filePath)
{
ArrayList<Item> itemz = new ArrayList<Item>();
try{
Scanner data = new Scanner(new File(filePath));
while(data.hasNextLine())
{
String next = data.nextLine();
if(!next.trim().isEmpty())
{
String[] dataz = next.split(",");
String name = dataz[0];
int value = Integer.parseInt(dataz[1]);
int weight = Integer.parseInt(dataz[2]);
itemz.add(new Item(name,value,weight));
}
}
data.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}
return itemz;
}