0

我是Java新手,所以提前感谢。我被要求编写一个名为 loadLeague() 的方法,该方法使用构造函数创建一个新的 League 对象,并用其详细信息包含在文本文件中的团队填充它。该方法应首先选择一个文件,然后读取文件的第一行并使用它创建一个 League 对象,该对象具有联赛名称和新对象的 teams 数组的正确大小。然后应该用新的 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();
 }   
4

0 回答 0