【问题标题】:Create object in an ArrayList from textfile input in java从 java 中的文本文件输入在 ArrayList 中创建对象
【发布时间】:2016-05-01 23:59:10
【问题描述】:

我有一个学生列表的文本文件,其中列出了姓氏、名字、实验成绩、项目成绩和考试成绩,例如:

Owens Will 46 54 56  
Smith John 44 77 99

我正在尝试编写一个读取文本文件的方法,使用每一行创建一个学生对象,然后将其添加到学生的 ArrayList 中。 Student 对象由名字、姓氏、实验室、项目和考试成绩组成。

这是我目前所拥有的:

private ArrayList<Student> arraylist = new ArrayList<Student>();

public void ReadFile(String inputfile) throws FileNotFoundException {
    File myFile = new File(inputfile);
    Scanner sc = new Scanner(myFile);

    while (sc.hasNextLine()) {
        arraylist.add(sc.nextLine());
    }
}

我不确定如何从文本文件创建 Student 对象,然后不确定如何将对象放入 ArrayList?

编辑:

这是我的学生班:

public class Student {
    // fields
    private String firstname;
    private String lastname;
    private int labgrade;
    private int projectgrade;
    private int examgrade;
    private int totalgrade;

    // constructor
    public Student(String firstname, String lastname, int labgrade,
        int projectgrade, int examgrade) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.labgrade = labgrade;
        this.examgrade = examgrade;
        this.totalgrade = labgrade + projectgrade + examgrade;
    }

    // method
    public String toString() {
        String s = firstname + " " + lastname + " has a total grade of "  + totalgrade;
        return s;
    }
}

【问题讨论】:

  • 你是如何/在哪里定义 Student 类的?
  • @ScottHunter 编辑显示

标签: java


【解决方案1】:

使用分割功能

String line = sc.nextLine();
String[] student = line.split(" ");
String lastName = student[0];
String firstName = student[1];
String labGrade = student[2];
String projectGrade = student[3];
String examGrade = student[4];

new Student(student[0],student[1],student[2],student[3],student[4]) String 对象中的 split 函数将拆分任何包含空格的子字符串,如上面的示例。您可以选择使用 String[] student = line.split(","); 在 CSV 文件中拆分例如逗号“,”,但在这种情况下它是空白空间。拆分将返回一个字符串数组

【讨论】:

    【解决方案2】:

    类似:

    public void ReadFile(String inputfile) throws FileNotFoundException {
        arraylist = new ArrayList<Student>();
        File myFile = new File(inputfile);
        Scanner sc = new Scanner(myFile);
    
        while (sc.hasNextLine()) {
            try {
                String[] line = sc.nextLine().split(" ");
    
                arraylist.add(new Student(line[1], line[0], Integer.parseInt(line[2]), ...));
            } catch (Exception e) {
                System.out.println("Error of some kind...");
                continue; // maybe, I dunno.
            }
        }
    }
    

    【讨论】:

    • 先索引 1 然后索引 0(因为它与姓氏名字相反)
    【解决方案3】:

    应该有效:

    private ArrayList<Student> arraylist = new ArrayList<Student>();
    
    public void ReadFile(String inputfile) throws FileNotFoundException 
    {
        File myFile = new File(inputfile);
        Scanner sc = new Scanner(myFile);
    
        while (sc.hasNextLine()) 
        {
            String[] stdinfo = sc.nextLine().split(" ");
            arraylist.add(new Student(stdinfo[1], stdinfo[0], Integer.parseInt(stdinfo[2]), Integer.parseInt(stdinfo[3]), Integer.parseInt(stdinfo[4])));
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多