Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
I don't understand very weel: do you want that variable clubsArr is an array of Object and not a ArrayList? Or another thing? –  alepuzio May 28 at 8:08
 
You want to read data from file and save it to array of objects? Please be more specific. –  Marek May 28 at 8:08
1  
For a more optimized code, the line String[] value = line.split(",", 3); should be outside of the for, it doesn't need to be splited each time. In fact, the whole for is not needed, you can do clubsArr.add(value);. –  Djon May 28 at 8:09
 
Yes i want to get rid of the Arraylist and have it read into an Array of Objects.. I have never worked with Arrays of objects before, so I don't know what I'm doing. Any help would be appreciated :) –  user2427023 May 28 at 8:13
add comment

3 Answers

Create a new Class that will hold the data of a row. In the easiest case you could create a class like this:

class MyClass {
     public String column1;
     public String column2;
     public ArrayList<String> aliases = new ArrayList<String>();

     public void addAliases(String[] a){
        for(int i=2;i<a.length;i++){
           aliases.add(a[i]);
        }
     }
}

Then change your ArrayList like so: ArrayList<MyClass> clubsArr = new ArrayList<MyClass>();

and your reading part like so:

while ((line = clubBR.readLine()) != null) { 

        MyClass club = new MyClass; 

        String[] value = line.split(",", 3);
        club.column1 = value[0]; 
        club.column2 = value[1]; 
        // etc...
        clubsArr.add(club); 

    }
    MyClass[] clubs = clubsArr.toArray();

That way you will later be able to get a value from one of the objects by using its attributename (in this case for example .column2) instead of some index you would have to keep in mind.

Note that you can call the attributes in the class to your liking (e.g. clubname instead of column1)

EDIT (to help with OPs edit) To check, if the file exists, replace the line BufferedReader clubBR = new BufferedReader(new FileReader(new File("nrlclubs.txt")));

with

File file = new File("nrlclubs.txt");
if(!file.exists()){
    System.exit(1); // no use to go any further if we have no input
}
BufferedReader clubBR = new BufferedReader(new FileReader(file));
share|improve this answer
 
OP said in a class called Clubs. –  johnchen902 May 28 at 8:14
2  
clubsArr.toArray() should be outside the loop. –  whoAmI May 28 at 8:15
1  
And better use a constructor like clubsArr.add(new Clubs(value[0],value[1],value[2])); with private member fields. –  Ravi Thapliyal May 28 at 8:15
 
Even better to parse the whole line to the constructor. –  Ashwin Mukhija May 28 at 8:18
 
@whoAmI Thanks. I gues that came from the speed. Edited –  LuigiEdlCarno May 28 at 8:18
show 5 more comments

I don't understand your question well but maybe you are looking for this:

public static void readClub() throws IOException {
        BufferedReader clubBR = new BufferedReader(new FileReader(new File("nrlclubs.txt")));

    String line = "";

    ArrayList<Clubs> clubs = new ArrayList<Clubs>();

    while ((line = clubBR.readLine()) != null) { 

        Clubs club = new Clubs();

        String[] value = line.split(",", 3);
        club.name = value[0]; 
        club.mascot = value[1];
        club.alias = value[2];

        clubs.add(club); 
    }

}

share|improve this answer
 
Thank you for the quick response. To help clarify what I am trying to do, i edited the original post –  user2427023 May 28 at 8:25
 
Does my answer satisfy you? –  Marek May 28 at 8:29
 
Um, sort of :) I'll be able to use it as a reference. Thanks for your help! –  user2427023 May 28 at 8:33
add comment

Create a class Clubs like this.

class Clubs {
    public String teamName;
    public String teamMascot;
    public String aliases;
    public Clubs(String csvLine) {
        String[] values = csvLine.split(',', 3);
        this.teamName = values[0];
        this.teamMascot = values[1];
        this.aliases = values[2];
    }
}

Then alter your code to this.

List<Clubs> clubList = new ArrayList<Clubs>();
while(line = clubsBR.readLine()) {
    Clubs temp = new Clubs(line);
    clubList.add(temp);
}
Clubs[] clubArr = clubList.toArray();

This should take care of everything. Encapsulation is left for you to implement, and you should do that. :)

share|improve this answer
 
Thanks for your help! I'm not too sure what this is for though. public Clubs(String csvLine) { where do I define csvLine? –  user2427023 May 28 at 8:35
 
It's passed from your while loop. Every line is passed to the constructor and then split into its correct place. –  Ashwin Mukhija May 28 at 8:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.