0

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.

4
  • I don't understand very weel: do you want that variable clubsArr is an array of Object and not a ArrayList? Or another thing? Commented May 28, 2013 at 8:08
  • You want to read data from file and save it to array of objects? Please be more specific. Commented May 28, 2013 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);. Commented May 28, 2013 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 :) Commented May 28, 2013 at 8:13

2 Answers 2

4

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));
Sign up to request clarification or add additional context in comments.

9 Comments

OP said in a class called Clubs.
clubsArr.toArray() should be outside the loop.
And better use a constructor like clubsArr.add(new Clubs(value[0],value[1],value[2])); with private member fields.
Even better to parse the whole line to the constructor.
@whoAmI Thanks. I gues that came from the speed. Edited
|
0

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); 
    }

}

3 Comments

Thank you for the quick response. To help clarify what I am trying to do, i edited the original post
Does my answer satisfy you?
Um, sort of :) I'll be able to use it as a reference. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.