【问题标题】:Create an Object and set its instance variables using bufferedScanner使用 bufferedScanner 创建一个对象并设置其实例变量
【发布时间】:2015-05-04 18:18:29
【问题描述】:

我试图从 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();                                               
             }
      }

【问题讨论】:

    标签: java arrays csv bufferedreader


    【解决方案1】:

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

    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() 无参数作为第一个语句,如果它不存在的话。所以你真的不需要调用超...你的超类是你的类中的对象

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      相关资源
      最近更新 更多