【发布时间】: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