By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

Iterating through a CSV file to create unique array objects?

Ispep
P: 4
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without saying I'm not asking you to solve the question - that won't help come exam time :().

Anyway I have a CSV delimited file in the following format;

Expand|Select|Wrap|Line Numbers
  1. STRING,INT
  2. STRING,INT,INT,INT,INT,INT
  3. STRING,INT,INT,INT,INT,INT
  4. STRING,INT,INT,INT,INT,INT
  5. [...]
The first two values relate to the league name and the number of teams in the league. The rest of the lines relate to the individual teams; their names, games won, drawn, lost, goals for and against.

The class method I'm writing; loadLeague() prompts the user for the text file of this csv, and after which (using BufferedReader and Scanner) reads the information from the first line to create a new League object as so;

Expand|Select|Wrap|Line Numbers
  1. public static League loadLeague()
  2.    {
  3.       League theLeague = null;
  4.       // to be completed by student
  5.       String pathname = OUFileChooser.getFilename();
  6.       File aFile = new File(pathname);
  7.       Scanner bufferedScanner = null;
  8.  
  9.       try
  10.       {
  11.       String leaguesName;
  12.       int numberOfTeams;
  13.       Scanner lineScanner;
  14.       String currentLine;
  15.  
  16.       bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
  17.  
  18.       currentLine = bufferedScanner.nextLine();
  19.       lineScanner = new Scanner(currentLine);
  20.       lineScanner.useDelimiter(",");
  21.  
  22.       leaguesName = lineScanner.next();
  23.       numberOfTeams = lineScanner.nextInt();
  24.       League League = new League(leaguesName,numberOfTeams);
     }
  25.       catch (Exception anException)
  26.       {
  27.          System.out.println("Error " + anException);
  28.       }
  29.       finally
  30.       {
  31.  
  32.       }
  33.  
  34.       return theLeague;
     }

*From line 5 onwards this is 'my' code.

The first String as I say is the name of the league, and the second is the number of teams. These values are passed as an argument to a private constructor League(String,Int) which aside from creating the League object creates a new (declared private) Teams array;

Expand|Select|Wrap|Line Numbers
  1. this.teams = new Team[size];
...to reference the Teams I'm expected to create and initialise - which is where I am having problems. I'm assuming I have to iterate in order to do this but how do I create dynamic variable names?

As an example of the (twisted) logic I've tried to apply;

Whilst File hasNextLine()
Store each next() and nextInt() value to temporary variables.
create new Team object
using public setters change Team objects values (won,drawn... ect)

This is the code I'm using to create the Team object;
Expand|Select|Wrap|Line Numbers
  1. Team myteam = new Team(theTeamsName);
I haven't got as far as to adding it to the Teams array yet as I can't get around the problem of unique names for each object? if I try;
Expand|Select|Wrap|Line Numbers
  1. Team theTeamsName = new Team(theTeamsName);
for example, it won't work as it's assigning the latter half to the variable (which is a String).

Is there any quick way around this? I mean surely there is a way for Java to understand I just want the contents of 'theTeamsName' and not the variable itself? That way I'm assuming that I can just access the Teams array and manipulate the referenced object by using setters?

Here is just one of my lame, half-assed, unfinished attempts to get around the problem, so see if you could tell me where I've gone wrong.

Expand|Select|Wrap|Line Numbers
  1. while (bufferedScanner.hasNextLine())
  2.        {       
  3.           currentLine = bufferedScanner.nextLine();
  4.           lineScanner = new Scanner(currentLine);
  5.           lineScanner.useDelimiter(",");
  6.  
  7.           tempTeamName = lineScanner.next();
  8.           won = lineScanner.nextInt();
  9.           drawn = lineScanner.nextInt();
  10.           lost = lineScanner.nextInt();
  11.           goalsFor = lineScanner.nextInt();
  12.           goalsAgainst = lineScanner.nextInt();
  13.           Team theTeam = new Team(tempTeamName);
  14.         }
Obviously I declared the variables earlier on (not shown). I believe this works (to the extent no error on processing) but obviously I can't tell yet and I'm not actually using any of the variables apart from teamName, which I could probably have done directly with just 'new Team(lineScanner.next());' right?

Just a fresh direction for me would be good. I'm lost on this!
May 16 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
Why don't you simply create a Team constructor that takes all the significant
parameters at once? Something like this:
Expand|Select|Wrap|Line Numbers
  1. public class Team {
  2.    private String name;
  3.    private int win;
  4.    private int lose;
  5.    private int draw;
  6.    private int gFor;
  7.    private int gAgainst;
  8.  
  9.    public Team(String name, int win, int lose, int draw, int gFor, int gAgainst) {
  10.       this.name= name;
  11.       this.win = win;
  12.       this.lose= lose;
  13.       this.draw= draw;
  14.       // etc.
  15.    }
  16.    ...
  17. }
In your leage class you read those six values using a scanner and construct
a next Team which you stick in your array:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < teams; i++) {
  2.    String name= scanner.next();
  3.    int win = scanner.nextInt();
  4.    // etc.
  5.    teams[i]= new Team(name, win, lose, draw, gFor, gAgainst);
  6. }
kind regards,

Jos
May 16 '07 #2

Ispep
P: 4
That would be great, but you aren't allowed to alter existing code, only add your own. The assignments are very much for learning a particular topic so it's quite strict.

Why don't you simply create a Team constructor that takes all the significant
parameters at once? Something like this:
Expand|Select|Wrap|Line Numbers
  1. public class Team {
  2.    private String name;
  3.    private int win;
  4.    private int lose;
  5.    private int draw;
  6.    private int gFor;
  7.    private int gAgainst;
  8.  
  9.    public Team(String name, int win, int lose, int draw, int gFor, int gAgainst) {
  10.       this.name= name;
  11.       this.win = win;
  12.       this.lose= lose;
  13.       this.draw= draw;
  14.       // etc.
  15.    }
  16.    ...
  17. }
In your leage class you read those six values using a scanner and construct
a next Team which you stick in your array:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < teams; i++) {
  2.    String name= scanner.next();
  3.    int win = scanner.nextInt();
  4.    // etc.
  5.    teams[i]= new Team(name, win, lose, draw, gFor, gAgainst);
  6. }
kind regards,

Jos
May 16 '07 #3

Expert 10K+
P: 11,448
Well, use those explicit mutators then:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (int i= 0; i < teams; i++) {
  3.    String name= scanner.next();
  4.    int win = scanner.nextInt();
  5.    // etc.
  6.    teams[i]= new Team(name);
  7.    teams[i].setWin(win);
  8.    teams[i].setLose(lose);
  9.    teams[i].setDraw(draw);
  10.    // etc.etc.
  11. }
Is that allowed?

kind regards,

Jos
May 16 '07 #4

Ispep
P: 4
Well, use those explicit mutators then:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (int i= 0; i < teams; i++) {
  3.    String name= scanner.next();
  4.    int win = scanner.nextInt();
  5.    // etc.
  6.    teams[i]= new Team(name);
  7.    teams[i].setWin(win);
  8.    teams[i].setLose(lose);
  9.    teams[i].setDraw(draw);
  10.    // etc.etc.
  11. }
Is that allowed?

kind regards,

Jos
Absolutely, in fact, that is what I had always intended but my Java knowledge must be so sloppy I didn't realise/remember you can assign objects to index references in the array. That takes care of the dynamic variable name, thanks a lot :)

My problem now is that I can't directly access the teams array because it's not static. So I create a new teams array like such (within loadLeague()); Team[] teams = new Team[numberOfTeams];

but even though the creation of this works within the Try block (println's confirm this) once the method is completed the League objects Teams array is empty. It has the correct size, but each entry is null

I'm guessing this is something to do with try blocks and/or static/non-static methods. I'm hoping someone can point me in the right direction?
May 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.