我是Java的新手,所以提前感谢。我被要求编写一个名为loadLeague()的方法,该方法使用构造函数创建一个新的League对象,并将其填充到其详细信息包含在文本文件中的团队。该方法应首先选择一个文件,然后读取第一行文件并使用它创建一个联盟对象,其中包含联盟名称和新对象的球队数组的正确大小。然后,团队引用的数组应填充新的Team对象,并根据文件中的数据设置实例变量 我的方法编译但我得到noSuchElementException。我的代码也没有填充数组。请任何人帮忙并指出我正确的方向?感谢
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());
}
}
要使用的文件:
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
Team类的代码
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();
}
答案 0 :(得分:0)
好的,这里的循环严重问题。
我重新编写代码,看看:
联盟课程
public class League //this is NOT a constructor.
{
private String name; // The name of the league
private Team[] teams=null; // An array to hold the teams in the league
private League(String aName, int size)
{
super();
this.name = aName;
this.teams = new Team[size];
}
public League() //this is a constructor.
{}
public League loadLeague()
{
League theLeague = null;
Scanner bufferedScanner = null;
try
{
String leagueName;
int numberOfTeams;
String teamName;
int won;
int drawn;
int lost;
int goalsFor;
int goalsAgainst;
Team aTeam = null;
Scanner lineScanner;
String currentLine;
Scanner in = new Scanner(new FileReader("ads.txt"));
//Just the name I gave to the .txt
////////////////////////////////////
// This JUST loads the league name and creates the array.
currentLine = in.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
leagueName = lineScanner.next();
numberOfTeams = lineScanner.nextInt();
theLeague = new League(leagueName,numberOfTeams);
teams = new Team[numberOfTeams];
///////////////////////////////////////
// Starting the loop
for (int i=0;i<numberOfTeams;i++)
{
currentLine = in.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
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());
teams[i] = aTeam; //save the team.
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
//you should close something here.
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
return theLeague;
}
public void display() //Errors here also. Take a look, it's easy!!
{
System.out.format("%20s %2s %2s %2s %2s %2s %2s %2s\n","","P","W","L","D","F","A","Pt");
for (int i=0;i<teams.length;i++)
{
System.out.format("%20s %2d %2d %2d %2d %2d %2d %2d\n",
teams[i].getName(), teams[i].getPlayed(),
teams[i].getWon(), teams[i].getDrawn(),
teams[i].getLost(), teams[i].getGoalsFor(),
teams[i].getGoalsAgainst(), teams[i].getPoints());
}
}
}
然后我做了一个Main课程来完成这项工作:
主要强>
public class Main {
public static void main(String[] args) {
League a = new League(); //this uses the blank constructor.
a.loadLeague();
a.display();
}
}
这是你的输出:
P W L D F A Pt
Airdrie United 16 3 2 11 14 25 11
Clyde 16 5 7 4 21 17 22
Dundee 16 7 2 7 21 18 23
Gretna 16 10 3 3 43 20 33
Hamilton Acas 16 7 5 4 19 20 26
Livingstone 16 6 6 4 21 15 24
Partick Thistle 16 8 4 4 25 29 28
Queen of South 16 3 3 10 11 31 12
Ross County 16 4 4 8 14 24 16
St Johnstone 16 6 6 4 26 16 24
可能有一些System.out和格式错误,我只是在测试,所以从代码中删除它们并按照你想要的方式进行更改。