I need to read a CSV file into an array of Objects. I didn't realise that it had to go into one, and just made an ArrayList instead. I now need to fix that, and have no idea what I'm doing.
This is my code to read the CSV file into a multidimensional array.
public static void readClub() throws IOException {
BufferedReader clubBR = new BufferedReader(new FileReader(new File("nrlclubs.txt")));
String line = "";
ArrayList<String[]> clubsArr = new ArrayList<String[]>();
while ((line = clubBR.readLine()) != null) {
String[] club = new String[3];
for (int i = 0; i < 3; i++) {
String[] value = line.split(",", 3);
club[i] = value[i];
}
clubsArr.add(club);
}
A snippet of my CSV file is this:
Glebe,G Shield,Glebe District
Cumberland,C Shield,Central Cumberland
Annandale,A Shield,Annandale District
University,Lion,Sydney Uni Rugby League Club
Souths,Rabbit,South Sydney,Rabbitohs
Easts,Rooster,Eastern Suburbs,Roosters,Sydney Roosters
Balmain,Tiger,Tigers,Balmain Tigers
Newtown,Jets,Jets,Newtown Jets,Bluebags
The first word is the Team name, the second word is the Team mascot, and the rest are the alias's.
Now the question is, how do i do the same thing, but with an Array of Objects (in a class called Clubs)? I just spent a few hours trying to get this working, only to be told its wrong, and the Array of Objects is doing my head in :'(
Thanks heaps!
edit: ok, the actual question is this: the program should read the content of the data file (NRLclubs.txt) into memory into an appropriate array of objects (see previous page for descriptions of the class). Do not assume that the file exists.
The description of the class is this: Club class: the Club class represents an individual NRL club within the competition. The Club class needs to store data for the current club name, current club mascot, any aliases by which the club is or has been known. As well as the normal methods that should be created for a class (eg, constructors, ‘setters’, and ‘getters’) you will need to decide upon appropriate methods for this class based upon the general requirements of the assignment specification.
String[] value = line.split(",", 3);
should be outside of thefor
, it doesn't need to be splited each time. In fact, the wholefor
is not needed, you can doclubsArr.add(value);
. – Djon May 28 at 8:09