I am new to Java so thanks in advance. I have been asked to write a method called loadLeague() which creates a new League object using the constructor and fill it with teams whose details are contained in a text file. The method should first select a file, then read the first line of file and use it to create a League object with the league name and a correct size for the new object's teams array. The array referenced by teams should then be filled with new Team objects with their instance variables set according to the data in the file My method compiles but I get noSuchElementException. My code also does not fill the array. Please can anyone help and point me in the right direction? Thanks
import ou.*;
import java.io.*;
import java.util.*;
/**
* Class League - An instance of this class represents the teams in a
* football (or similar) league. It provides a class method for creating
* a new instance of League by reading the data for the teams from a CSV
* file.
*
* @author
* @version 1.0
*/
public class League
{
/* instance variables */
private String name; // The name of the league
private Team[] teams; // An array to hold the teams in the league
/**
* Constructor for objects of class League. It sets the name of the league
* to the String object provided as the first argument and initialises teams
* to an array of the size provided as the second argument. This constructor
* is private as it is intended for use only by the class method loadLeague().
*/
private League(String aName, int size)
{
super();
this.name = aName;
this.teams = new Team[size];
}
/* class method */
/**
* This method creates a new League object by reading the required details from
* a CSV file. The file must be organised as follows:
* name(String), number of teams (int)
* team name(String), won(int), drawn(int), lost(int), for(int), against(int)
* and so on for each team
* Having created the new League object the method should create all the Team
* objects (using the data in the file to set their attributes) and add them
* to the teams array.
*/
public static League loadLeague()
{
League theLeague = null;
String pathname = OUFileChooser.getFilename();
File aFile = new File(pathname);
Scanner bufferedScanner = null;
try
{
String leagueName;
int numberOfTeams;
String teamName;
int won;
int drawn;
int lost;
int goalsFor;
int goalsAgainst;
Scanner lineScanner;
String currentLine;
bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
while (bufferedScanner.hasNextLine())
{
currentLine = bufferedScanner.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
leagueName = lineScanner.next();
numberOfTeams = lineScanner.nextInt();
theLeague = new League(leagueName,numberOfTeams);
Team aTeam = new Team(lineScanner.next());
aTeam.setWon(lineScanner.nextInt());
aTeam.setDrawn(lineScanner.nextInt());
aTeam.setLost(lineScanner.nextInt());
aTeam.setGoalsFor(lineScanner.nextInt());
aTeam.setGoalsAgainst(lineScanner.nextInt());
Team[] teams = new Team[numberOfTeams];
teams[numberOfTeams] = aTeam;
numberOfTeams++;
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedScanner.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
return theLeague;
}
/* instance methods */
/**
* Displays the league table in tabular format to the standard output
*/
public void display()
{
System.out.println(this.name);
System.out.format("%20s %2s %2s %2s %2s %2s %2s %2s\n","","P","W","L","D","F","A","Pt");
for (Team eachTeam : this.teams)
{
System.out.format("%20s %2d %2d %2d %2d %2d %2d %2d\n",
eachTeam.getName(), eachTeam.getPlayed(),
eachTeam.getWon(), eachTeam.getDrawn(),
eachTeam.getLost(),eachTeam.getGoalsFor(),
eachTeam.getGoalsAgainst(), eachTeam.getPoints());
}
}
File that is to be used:
Scottish League Division 1,10
Airdrie United ,3,2,11,14,25
Clyde ,5,7,4,21,17
Dundee ,7,2,7,21,18
Gretna ,10,3,3,43,20
Hamilton Acas ,7,5,4,19,20
Livingstone ,6,6,4,21,15
Partick Thistle,8,4,4,25,29
Queen of South ,3,3,10,11,31
Ross County ,4,4,8,14,24
St Johnstone ,6,6,4,26,16
code for Team class
public class Team
{
/* instance variables */
private String name;
private int won; // the number of games won
private int drawn; // the number of games drawn
private int lost; // the number of games lost
private int goalsFor; // the number of goals scored by the team
private int goalsAgainst; // the number of goals scored against the team
/**
* Constructor for objects of class Team. It sets the name of the team
* to that provided by the argument.
*/
public Team(String aName)
{
super();
this.name = aName;
}
/* instance methods */
/**
* Set the number of games won
*/
public void setWon(int aWon)
{
this.won = aWon;
}
/**
* Set the number of games drawn
*/
public void setDrawn(int aDrawn)
{
this.drawn = aDrawn;
}
/**
* Set the number of games lost
*/
public void setLost(int aLost)
{
this.lost = aLost;
}
/**
* Set the number of goals scored by the team
*/
public void setGoalsFor(int aGoalsFor)
{
this.goalsFor = aGoalsFor;
}
/**
* Set the number of games scored against the team
*/
public void setGoalsAgainst(int aGoalsAgainst)
{
this.goalsAgainst = aGoalsAgainst;
}
/**
* Return the name of the team
*/
public String getName()
{
return this.name;
}
/**
* Return the number of games won
*/
public int getWon()
{
return this.won;
}
/**
* Return the number of games drawn
*/
public int getDrawn()
{
return this.drawn;
}
/**
* Return the number of games lost
*/
public int getLost()
{
return this.lost;
}
/**
* Return the number of goals scored by the team
*/
public int getGoalsFor()
{
return this.goalsFor;
}
/**
* Return the number of goals scored against the team
*/
public int getGoalsAgainst()
{
return this.goalsAgainst;
}
/**
* Return the number of games played
*/
public int getPlayed()
{
return this.getWon() + this.getDrawn() + this.getLost();
}
/**
* Return the goal difference
*/
public int getGoalDifference()
{
return this.getGoalsFor() - this.getGoalsAgainst();
}
/**
* Return the number of points gained
*/
public int getPoints()
{
return this.getWon() * 3 + this.getDrawn();
}