如何遍历数组并将其放入对象

时间:2019-07-21 09:52:44

标签: java arrays object

我想遍历我拥有的数组并将每个值分配给对象中的变量

对不起,我想更清楚地说明这一点。

我正在从.txt文件中读取内容,需要将5位学生的值插入对象。

文本文件是一个.csv文件,

  

1,Frank,West,98,95,87,78,77,80

     

2,黛安,格林,78,94,88,87,95,92

     

3,Doug,Lei,78,94,88,87,95,92

     

4,詹姆斯,惠普,69,92,74,77,89,91

     

5,Aroha,Wright,97,92,87,83,82,92`

理想情况下,我想创建一个循环,遍历每个字段并创建一个新的Student对象。

代码段是我提供的示例代码,但经过数小时的尝试后我无法使它工作。

基本上,我想我是在问addstudent方法应如何工作,因为该方法将这些值放入一个对象中。

readFile()方法的代码:

/**
* Reads a text file containing student data and uses this to populate the student objects
*
* @param filename Path to the student data file to be read
* @return Whether the file was read successfully
*/
public static boolean readFile(String filename) { 
      File file = new File(filename);
      try {
           Scanner scanner = new Scanner(file);
           while(scanner.hasNextLine()){
               String[] words = scanner.nextLine().split(",");
               addStudent(id,firstName,lastName,mathMark1,.....); // TODO: Finish adding the parameters
             }
          scanner.close();
       } catch (FileNotFoundException e) { 
             System.out.println("Failed to read file");
       } 

    return true;
}

studentdata.txt的数据

4 个答案:

答案 0 :(得分:0)

假设已设置文本文件,使其与您的addStudent(..)方法中的参数匹配,那么您可以使用来传递参数

addStudent(words[0], words[1], words[2], ...);

答案 1 :(得分:0)

您的问题不清楚。..根据您的编码风格,我认为您尝试从文本文件中读取数据并将所有数据存储到集合对象中。...

根据我的假设,您可以通过使用流动代码来做到这一点...

public class Student {

    private String id;
    private String firstName;
    private String lastName;

    public Student(){}

    //all args constructor 
    public Student(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // all getter and setter code
}

将您的代码更改为

List<Student> list = new ArrayList<>();
 while(scanner.hasNextLine()){
               String[] words = scanner.nextLine().split(",");
               list.add(new Student(words[0],words[1],words[2])); // TODO: Finish adding the parameters
  }

答案 2 :(得分:0)

首先,创建一个课程学生

public class Student {
  public String id, first_name,last_name;
  //assuming it was subject grades
  public String[] subject_grades;

  public void Student(String id,String first_name,String last_name,String subject_grades[]){
     this.id = id;
     this.first_name = first_name;
     this.last_name = last_name;
     this.subject_grades = subject_grades;


  }
}

然后使用您的主要方法

List<Student> student_list = new ArrayList<>();
while(scanner.hasNextLine()){
   String[] words = scanner.nextLine().split(",");
   student_list.add(new Student(words[0],words[1],words[2], new String[] {words[3],words[4],words[5],words[6],words[7]} ));
}

答案 3 :(得分:0)

仅需您的最后一条评论即可:

  1. 创建您的学生班级,如下所示:

    class Student {
       private String firstName;
    
       public Student(String firstName) {
         this.firstName = firstName;
       }
    
    }
    
  2. 创建一个集合来容纳所有学生对象(在此处使用列表)

    static List<Student> listOfStudents = new ArrayList<>();
    
  3. 现在将addStudent()定义为:

    static void addStudent(Student std) {
       listOfStudents.add(std);
    }
    
  4. 您可以在读取每一行后调用此方法

    while (scanner.hasNextLine()) {
        String[] words = scanner.nextLine().split(",");
        // create an object of Student with first-name and pass it to addStudent
        addStudent(new Student(words[1]));
    }