1

I have a text file of a list of Students that lists last name, first name, lab grade, project grade, and exam grade, for example:

Owens Will 46 54 56  
Smith John 44 77 99

I am trying to write a method that reads the textfile, uses each line to create a Student object and then adds that to an ArrayList of Students. A Student object consists of the first name, last name, lab, project, and exam grades.

Here's what I have so far:

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());
    }
}

I'm unsure of how to create the Student object from the text file and then unsure of how to place the objects into the ArrayList?

EDIT:

Here's my Student class:

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;
    }
}
3
  • 1
    How/where did you define the Student class? Commented May 2, 2016 at 0:03
  • 1
    String#split Commented May 2, 2016 at 0:11
  • @ScottHunter edited to show Commented May 2, 2016 at 0:13

3 Answers 3

1

use split function

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];

or new Student(student[0],student[1],student[2],student[3],student[4]) the function split in String object will split any substring containing empty space like the above example. You could choose to split for example comma "," in CSV file using String[] student = line.split(","); but in this case it is empty space. Split will return an array of string

Sign up to request clarification or add additional context in comments.

Comments

0

Something like:

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 Comment

index 1 first then index 0 (since it is opposite last name first name)
0

Should work:

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])));
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.