繁体   English   中英

创建一个对象并使用bufferedScanner设置其实例变量

[英]Create an Object and set its instance variables using bufferedScanner

我试图从csv文件中读取文本,使用lineScanner分离出值,然后使用第一个标记创建一个对象,并将该类的实例变量设置为以下标记。 然后,我必须使用这些变量使用已经创建的方法来计算球队总得分,然后将Team对象存储到球队所引用的数组中。

我所能做的就是将csv文件分开,但是不确定如何从此处继续。

班组

public class Team implements Comparable<Team>
{
   private String name;
   private int won; 
   private int drawn; 
   private int lost; 
   private int fourOrMoreTries; 
   private int sevenPointsOrLess; 
   private int totalPoints; 

   public Team(String aName)
   {
      super();
      this.name = aName;

班级池

public class Pool
{
   /* instance variables */
   private String poolName; // the name of the pool
   private Team[] teams;    // the teams in the pool
   private final static int NOOFTEAMS = 5; // number of teams in each pool

   /**
    * Constructor for objects of class Pool
    */
   public Pool(String aName)
   {
      super();
      this.poolName = aName;
      this.teams = new Team[NOOFTEAMS];

public void loadTeams()
   {
      String fileName;
      OUDialog.alert("Select input file for " + this.getPoolName());
      fileName = OUFileChooser.getFilename();
      File aFile = new File(fileName);
      Scanner bufferedScanner = null;

      try
      {
         String teamName;
         int teamWon;
         int teamDrawn;
         int teamLost;
         int teamFourOrMoreTries;
         int teamSevenPointsOrLess;
         Scanner lineScanner;
         String currentLine;
         bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
         currentLine = bufferedScanner.nextLine();

         if (!poolName.equals(currentLine))
         {
           OUDialog.alert("Wrong File Selected");           
         }
         else
         {
            while (bufferedScanner.hasNextLine())
         {
               currentLine = bufferedScanner.nextLine();
               lineScanner = new Scanner(currentLine);            
               lineScanner.useDelimiter(",");
               teamName = lineScanner.next();
               teamWon = lineScanner.nextInt();
               teamDrawn = lineScanner.nextInt();
               teamLost = lineScanner.nextInt();
               teamFourOrMoreTries = lineScanner.nextInt();
               teamSevenPointsOrLess = lineScanner.nextInt();                                               
             }
      }

关于您发布的代码,我有几件事要说。

1)团队班级不完整。 如果需要,可以使构造函数使用更多参数,以便可以创建不带getter / setter的Team实例。

例如:

public Team(String aName, int... ints) {
  this.won = ints[0];
  this.name = aName;
  this.drawn = ints[1]; 
 // and so on....
}

然后在您的泳池班级中创建球队名单,

List<Team> listofTeams = new ArrayList<Team>();

在您结束时:

while (bufferedScanner.hasNextLine())
     {
           currentLine = bufferedScanner.nextLine();
           lineScanner = new Scanner(currentLine);            
           lineScanner.useDelimiter(",");
           teamName = lineScanner.next();
           teamWon = lineScanner.nextInt();
           teamDrawn = lineScanner.nextInt();
           teamLost = lineScanner.nextInt();
           teamFourOrMoreTries = lineScanner.nextInt();
           teamSevenPointsOrLess = lineScanner.nextInt();                                               
         }
       listofTeams.add(new Team(teamName, teamWon, teamDrawn, ... add more ints);

2)你知道什么super(); 有吗 您是正确的,但它是必需的,但如果super是Object,则不需要,如果super不带任何参数,则不需要。 假设您没有在构造函数中的第一个语句调用super()no parameter,如果它在那里没有。 因此,您真的不需要调用超级类。您的超级类是您类中的Object

您还需要做更多的事情。 您的学校有办公时间吗? 如果他们这样做,他们可以为您提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM