分离数据并存储在ArrayList中?
Separate data and store in ArrayList?
我正在尝试分离一组数据。我需要将每个数据存储到一个变量中,然后创建一个对象并将它们放入 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);
}
}
//for list of courses
这是一个字符串列表。
这是课程列表
List<Course> list = new ArrayList<>();
然后,只是 list.add(ci)
在循环中
旁注:Student.addCourses
应该是单数,并且可能接受 Course
对象
首先,您需要一个列表来存储您的学生:
ArrayList<Student> students = new ArrayList<>();
在解析完所有字段后,您应该将每个创建的学生添加到此列表中:
students.add(s1);
其次,我认为最好将 listCourses
字段从 ArrayList<String>
更改为 ArrayList<Course>
- 以便在 Student
对象中存储有关所有学生课程的信息。
第三,您不需要将 coursesTaken
乘以 2,因为您为每门课程调用 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
}
更新: 学生的变化 class:
public class Student {
private String name;
private String id;
private ArrayList<Course> listCourses = new ArrayList<>();
...
// this is just a setter for listCourses field
public void setListCourses(ArrayList<Course> listCourses) {
this.listCourses = listCourses;
}
}
我正在尝试分离一组数据。我需要将每个数据存储到一个变量中,然后创建一个对象并将它们放入 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);
}
}
//for list of courses
这是一个字符串列表。
这是课程列表
List<Course> list = new ArrayList<>();
然后,只是 list.add(ci)
在循环中
旁注:Student.addCourses
应该是单数,并且可能接受 Course
对象
首先,您需要一个列表来存储您的学生:
ArrayList<Student> students = new ArrayList<>();
在解析完所有字段后,您应该将每个创建的学生添加到此列表中:
students.add(s1);
其次,我认为最好将 listCourses
字段从 ArrayList<String>
更改为 ArrayList<Course>
- 以便在 Student
对象中存储有关所有学生课程的信息。
第三,您不需要将 coursesTaken
乘以 2,因为您为每门课程调用 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
}
更新: 学生的变化 class:
public class Student {
private String name;
private String id;
private ArrayList<Course> listCourses = new ArrayList<>();
...
// this is just a setter for listCourses field
public void setListCourses(ArrayList<Course> listCourses) {
this.listCourses = listCourses;
}
}