我正在尝试分离一组数据。我需要将每个数据存储到一个变量中,然后创建一个对象并将它们放在一个ArrayList中。
数据集采用此格式
lastName, firstName
ID num_courses
course1_name
grade units
course2_name
grade units
....
到目前为止,我已经完成了这项工作,但我在创建多个对象,存储数据以及将其添加到ArrayList []时遇到了问题。感谢您的时间和帮助(:
public static void main (String[] args) throws FileNotFoundException {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = scnr.nextLine();
Scanner inFile = new Scanner(new FileInputStream(fileName)); //importing filename
ArrayList<String> list = new ArrayList<String>(); //for list of courses
String name = "";
String id = "";
int coursesTaken = 0;
String courseName = "";
char grade;
int courseUnit = 0;
while(inFile.hasNextLine()){
name = inFile.nextLine(); //first line is name
StringTokenizer st = new StringTokenizer(inFile.nextLine()); //separting whitespace
id = st.nextToken(); //storing ID
coursesTaken = Integer.parseInt(st.nextToken()); //storing num_courses_taken
Student s1 = new Student(name, id); //FIXME (need more than 1 student object)
for(int i = 0; i < coursesTaken*2; ++i){
courseName = inFile.nextLine();
s1.addCourses(courseName); //adding to arrayList
StringTokenizer st2 = new StringTokenizer(inFile.nextLine());
grade = st2.nextToken().charAt(0);
courseUnit = Integer.parseInt(st2.nextToken());
Course ci = new Course(courseName, grade, courseUnit); //FIXME (need more than 1 course)
}
}
}
Course.java
public class Course {
private String name;
private char grade;
private int units;
//constructors
public Course(){
name = "";
grade = 0;
units = 0;
}
public Course(String name, char grade, int units){
this.name = name;
this.grade = grade;
this.units = units;
}
//getters
public String getName(){
return name;
}
public char getGrade(){
return grade;
}
public int getUnits(){
return units;
}
//setters
public void setName(String name){
this.name = name;
}
public void setGrade(char grade){
this.grade = grade;
}
public void setUnits(int units){
this.units = units;
}
}
Student.java
public class Student {
private String name;
private String id;
private ArrayList<String> listCourses = new ArrayList<String>();
//constructor
//default
public Student(){
name = "none";
id = "none";
//courses = "none";
}
public Student(String name, String id){
this.name = name;
this.id = id;
//this.courses = courses;
}
//getters
public String getName(){
return name;
}
public String getId(){
return id;
}
public ArrayList<String> getCourses(){
return listCourses;
}
//setters
public void setName(String name){
this.name = name;
}
public void setId (String id){
this.id = id;
}
public void addCourses(String courses){
listCourses.add(courses);
}
}
答案 0 :(得分:0)
//用于课程列表
这是一个字符串列表。
这是一个课程列表
List<Course> list = new ArrayList<>();
然后,只在循环中list.add(ci)
旁注:Student.addCourses
应该是单数,并且可能接受Course
对象
答案 1 :(得分:0)
首先,您需要一个列表来存储您的学生:
18/01/30 03:35:45 INFO tool.ImportTool: Destination directory wasb://sample@workingclusterblob.blob.core.windows.net/source/employ is not present, hence not deleting.
18/01/30 03:35:45 INFO mapreduce.ImportJobBase: Beginning query import.
18/01/30 03:35:46 INFO client.AHSProxy: Connecting to Application History server at headnodehost/10.0.0.19:10200
18/01/30 03:35:46 ERROR tool.ImportTool: Encountered IOException running import job: org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory wasb://sample@workingclusterblob.blob.core.windows.net/source/employ already exists
在解析所有字段时,您应该将每个创建的学生添加到此列表中:
ArrayList<Student> students = new ArrayList<>();
其次,我认为,最好将students.add(s1);
字段从listCourses
更改为ArrayList<String>
- 以存储有关ArrayList<Course>
对象内所有学生课程的信息。
第三,您不需要将Student
乘以2,因为您为每门课程致电coursesTaken
两次。
inFile.nextLine()
最后你的主要方法如下:
for (int i = 0; i < coursesTaken; i++)
更新:学生班级的变化:
private ArrayList<Course> listCourses = new ArrayList<>();
ArrayList<Student> students = new ArrayList<>();
while(inFile.hasNextLine()){
name = inFile.nextLine(); //first line is name
StringTokenizer st = new StringTokenizer(inFile.nextLine()); //separting whitespace
id = st.nextToken(); //storing ID
coursesTaken = Integer.parseInt(st.nextToken()); //storing num_courses_taken
Student s1 = new Student(name, id);
ArrayList<Course> courses = new ArrayList<>();
for(int i = 0; i < coursesTaken; i++){
courseName = inFile.nextLine();
StringTokenizer st2 = new StringTokenizer(inFile.nextLine());
grade = st2.nextToken().charAt(0);
courseUnit = Integer.parseInt(st2.nextToken());
courses.add(new Course(courseName, grade, courseUnit)); //FIXED
}
s1.setListCourses(courses);
students.add(s1); //FIXED
}