-1

I am trying read text from a .txt file called "Marks.txt" (I attached the file here-> Marks.txt) which contains student data, Like this, thefirst 2 rows are headers or just labels for the data

The first two lines are just headers or labels as stated above, and I'm trying to skip or ignore these lines because I only need to create objects from the actual int and float data in the .txt file. Here is my loadFrom method in my Student class:

public static Student loadFrom2(BufferedReader aFile) throws IOException {
Student newStudent = new Student();

StringTokenizer st = new StringTokenizer(aFile.readLine());

newStudent.ID = Integer.parseInt(st.nextToken());
newStudent.a1 = Float.parseFloat(st.nextToken());
newStudent.a2= Float.parseFloat(st.nextToken());
newStudent.a3 = Float.parseFloat(st.nextToken());
newStudent.a4 = Float.parseFloat(st.nextToken());
newStudent.midterm = Float.parseFloat(st.nextToken());
newStudent.finalExam = Float.parseFloat(st.nextToken());

return (newStudent);

}

and here is my test file that reads the data:

 private static void readTest2() throws IOException {
    BufferedReader  file1;
    Student         student1;
    String line;

    file1 = new BufferedReader(new FileReader("C:\\Marks.txt"));
    student1 = Student.loadFrom2(file1);
    System.out.println(student1);

}

public static void main(String[] args) throws IOException {
    readTest2();
}

I took the Marks.txt file and remove the first two lines (headers or column labels) and was able to properly use the BufferedReader and StringTokenizer in my loadFrom2 method to read the .txt file in my readTest2 file.

Can any of you guys tell me how to skip the first 2 lines? I'm supposed to be reading from Marks.txt and creating a Student object in the loadFrom2 method for each line of data in the Marks.txt file. I'm supposed to then have another class called CourseSection is has an ArrayList of objects from the previous class.

Please let me know if I need to clarify anything. Here are the full class files if you want to see:

Student.java Class

CourseSection.java Class

StudentSaveLoadTest *to test reading the Marks.txt file

Neuron
  • 5,141
  • 5
  • 38
  • 59
reflexez
  • 1
  • 1
  • 3

1 Answers1

0

You can use BufferedReader.readLine() to consume a line of the input and just don't do anything with it.

Maybe change your code to something like this:

public static Student loadFrom2(String studentLine) throws IOException {
    Student newStudent = new Student();

    StringTokenizer st = new StringTokenizer(studentLine);

    newStudent.ID = Integer.parseInt(st.nextToken());
    newStudent.a1 = Float.parseFloat(st.nextToken());
    newStudent.a2 = Float.parseFloat(st.nextToken());
    newStudent.a3 = Float.parseFloat(st.nextToken());
    newStudent.a4 = Float.parseFloat(st.nextToken());
    newStudent.midterm = Float.parseFloat(st.nextToken());
    newStudent.finalExam = Float.parseFloat(st.nextToken());

    return (newStudent);
}

private static void readTest2() throws IOException {

    BufferedReader file1 = new BufferedReader(new FileReader("C:\\Marks.txt"));

    file1.readLine();
    file1.readLine();

    String line = file1.readLine();
    while(line != null){
        Student student1 = Student.loadFrom2(line);
        System.out.println(student1);
        line = file1.readLine();
    }
}
xtratic
  • 4,600
  • 2
  • 14
  • 32
  • Interesting. I just tried it and it works. My Student is actual an object and my CourseSection is an ArrayList of Student objects. I'm pretty sure my Prof. specified us to read the Mark.txt 1 line at a time in the Student.java class to create 1 Student OJBECT at a time, so I shouldn't need the while loop. THEN, I just have to incorporate this into the CourseSection, because it will use the Student objects in its arraylist. Thank you for your help! – reflexez Apr 22 '18 at 00:46