Arraylist混淆从文件中读取数据

时间:2014-12-08 13:23:53

标签: java file-io arraylist

我不能让我的生活得到这个程序来编译。

这是确切的任务:

关于上述uml图的几点:

addScore(double pScr)//是一种便捷方法,可以将一个分数添加到分数ArrayList中作为时间。

calculateTotalScore()//是另一种方便的方法,根据以下关于如何得出总得分的规则来计算潜水员的总得分。

toString()// build'一个字符串,由Diver的名字和姓氏以及分数ArrayList和Diver'中包含的所有Diver个人得分组成。得分总分。

我为您提供了DiverTester课程,可以有效地测试您的Diver课程。此类将读取提供的数据文件,创建Diver对象的实例,并在数据从数据文件中读取时填充它们。 DiverTester类将创建的Diver对象存储在它自己的ArrayList中。 一旦读取了所有数据并且已经填充了Diver对象的ArrayList,就执行与赋值10相同的处理。对于每个Diver,使用提供的评分规则输出潜水员的姓名和总分。通过丢弃最低和最高分数然后添加剩余分数来计算每个参赛者的分数。将每个潜水员的总分数格式化为两位小数。例如,陈若琳的输出将是:陈若琳 - 56.90分。

现在,我知道我的情况很糟糕,但我不知道从哪里开始,请帮忙!

Here is the uml file I was given

以下是教授为测试人员文件提供的代码:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class DiverTester {

  static ArrayList<Diver> divers = new ArrayList<Diver>();

  public static void main(String[] args) throws IOException {
    loadDivers(); //call method to read and load the Diver data
    outputDivers();
  }

  public static void loadDivers() throws IOException {
    Scanner infile  = new Scanner(new FileReader("diving_data.txt"));
    double scr = 0.0;
    String str = null;

    while (infile.hasNext()) {
      Diver dvr = new Diver();
      str = infile.next(); //read first bit of data in row
      dvr.setFirstName(str);
      dvr.setLastName(infile.next());  //read next bit of data in row

      //for loop reads one diver's scores in
      for(int c = 0; c < 8; c++) {
        dvr.addScore(infile.nextDouble());
      }
      divers.add(dvr);
    }
  }

  public static void outputDivers() throws IOException {
    for(Diver dvr : divers) {
      System.out.println(dvr.toString() + "\n");
    }
  }

}

到目前为止我的代码:

import java.util.ArrayList;

public class Diver
{
  public String firstName;
  public String lastName;
  public ArrayList scores = new ArrayList<Double>();

  public Diver()
  {
  }
  public Diver(String pFirst, String pLast, ArrayList scores)
  {
    firstName = "";
    lastName = "";

  }
  public void setFirstName(String pFirst)
  {
    firstName = pFirst;
  }
  public String getFirstName()
  {
    return firstName;
  }
  public String getLastName()
  {
    return lastName;
  }
  public void setLastName(String pLast)
  {
    lastName = pLast;
  }
  public Arraylist getScores()
  {
    return scores;
  }
  public void setScores(ArrayList scores)
  {
    scores = pScr;
  }
  public void addScore(double pScr)
  {
    scores.add(pScr);
  }
  public double calculateTotalScore()
  {
    if (scores.size() < 3) {
            return 0.0;
        }
        else {
            double sum = 0.0;
            int counter = 0;
            for(double score : scores) {
                if (counter != 0 && counter != scores.size() - 1) {
                    sum += score;
                }
                counter++;
            }
            return sum;
  }
}
  public String toString(){
    StringBuffer buf = StringBuffer();
    buf.append("\nDiver\n");
    buf.append("Name: " + getFirstName() + " "+ getLastName() + "\n");
    buf.append("Scores: " + getScores().toString());
    buf.append("Total Score: " + Double.calculateTotalScore(scores).toString());
    return buf.toString();
  }
}


Errors: 
--------------
File: /Users/gcaruso/Documents/Diver.java  [line: 34]
Error: /Users/gcaruso/Documents/Diver.java:34: cannot find symbol
symbol  : class Arraylist
location: class Diver
File: /Users/gcaruso/Documents/Diver.java  [line: 40]
Error: /Users/gcaruso/Documents/Diver.java:40: cannot find symbol
symbol  : variable pScr
location: class Diver
File: /Users/gcaruso/Documents/Diver.java  [line: 54]
Error: /Users/gcaruso/Documents/Diver.java:54: incompatible types
found   : java.lang.Object
required: double
File: /Users/gcaruso/Documents/Diver.java  [line: 64]
Error: /Users/gcaruso/Documents/Diver.java:64: cannot find symbol
symbol  : method StringBuffer()
location: class Diver
File: /Users/gcaruso/Documents/Diver.java  [line: 68]
Error: /Users/gcaruso/Documents/Diver.java:68: cannot find symbol
symbol  : method calculateTotalScore(java.util.ArrayList)
location: class java.lang.Double
-------------

2 个答案:

答案 0 :(得分:0)

在你的潜水员Class'toString()方法中,它应该是StringBuffer buf = new StringBuffer()而不是StringBuffer buf = StringBufferr(); 试试这个:ArrayList<double> scores = new ArrayList<double>();

答案 1 :(得分:0)

我会查看代码的这四个部分。

1)

  public void setScores(ArrayList scores)
  {
    scores = pScr;
  }

方法的主体可以是    this.score =得分 我不知道你从哪里得到pScr。

2)

StringBuffer buf = StringBuffer();

应该是    StringBuffer buf = new StringBuffe();

3)

Double.calculateTotalScore(scores)

应该是     calculateTotalScore(); 4)

public ArrayList scores = new ArrayList<Double>();

应该是     public ArrayList scores = new ArrayList();