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

时间:2015-05-04 18:18:29

标签: java arrays csv bufferedreader

我尝试从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 个答案:

答案 0 :(得分:0)

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

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不带参数则不是。假设您在构造函数中调用了super()no参数作为第一个语句,如果它不存在的话。所以你真的不需要调用超级...你的超级类是你班级中的对象

还有很多事情需要处理。你的学校有办公时间吗?如果他们这样做,他们可以帮助你。