user2427023
user2427023

Reputation: 107

JAVA - How to create an array of objects?

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.

Upvotes: 0

Views: 5285

Answers (2)

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

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

Upvotes: 4

Marek
Marek

Reputation: 4081

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

}

Upvotes: 0

Related Questions