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;
- STRING,INT
-
STRING,INT,INT,INT,INT,INT
-
STRING,INT,INT,INT,INT,INT
-
STRING,INT,INT,INT,INT,INT
-
[...]
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;
- public static League loadLeague()
-
{
-
League theLeague = null;
-
// to be completed by student
-
String pathname = OUFileChooser.getFilename();
-
File aFile = new File(pathname);
-
Scanner bufferedScanner = null;
-
-
try
-
{
-
String leaguesName;
-
int numberOfTeams;
-
Scanner lineScanner;
-
String currentLine;
-
-
bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
-
-
currentLine = bufferedScanner.nextLine();
-
lineScanner = new Scanner(currentLine);
-
lineScanner.useDelimiter(",");
-
-
leaguesName = lineScanner.next();
-
numberOfTeams = lineScanner.nextInt();
-
League League = new League(leaguesName,numberOfTeams);
}
- catch (Exception anException)
-
{
-
System.out.println("Error " + anException);
-
}
-
finally
-
{
-
-
}
-
-
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;
- 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;
- 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;
- 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.
- while (bufferedScanner.hasNextLine())
-
{
-
currentLine = bufferedScanner.nextLine();
-
lineScanner = new Scanner(currentLine);
-
lineScanner.useDelimiter(",");
-
-
tempTeamName = lineScanner.next();
-
won = lineScanner.nextInt();
-
drawn = lineScanner.nextInt();
-
lost = lineScanner.nextInt();
-
goalsFor = lineScanner.nextInt();
-
goalsAgainst = lineScanner.nextInt();
-
Team theTeam = new Team(tempTeamName);
-
}
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!