Home > Back-end >  Reading/splitting different parts from different lines in a text file in Java
Reading/splitting different parts from different lines in a text file in Java

Time:11-06

I'm working on this assignment I'm supposed to read from a text file like this...

Student Name: John
Student ID: 12344/19
College: Science

Credits Attempted: 15
Credits Earned: 15
Grade Points: 41.2
Course Code Course Title            Credit      Grade
COMP1007,   Amazing Applications of AI,  2,      B
COMP2202,   Fund. of Object Oriented Prog.,  3,      C-
MATH2108,   Calculus (2),            3,          C-
MATH3340,   Discrete Math. for Comp. Sci.,   3,          B-
STAT2101,   Introduction to Statistics,      4,          C 

I should read this text file and calculate the GPA of the student and create an output file that should look like this...

Output text file

So basically I'm stuck and I have no idea what I to do... I know how to read line by line and split a line into different parts, but this doesn't seem to work here since every line is different from the other. For example the first line has two parts, the "Student Name" and the name itself in this case "John". But in line 9, there are four different parts, the course code, course name, credit and grade.

I'm honestly not looking to cheat on the assignment but only to understand it

help :)

Note I can't use Stream or Hashmap or BufferedReader

CodePudding user response:

OK, so here's how you do it ...

  1. You read in all of the file and store each line in a List<String>
  2. For the first 8 lines you process each one in a separate way. You can even write a separate function to parse the necessary info out of every line for lines 0-7
  3. All the remaining lines have identical structure. Therefore, you can process them all in the same way to parse out and then process the necessary data.

And a comment to this answer if something is unclear and I'll clarify.

  • Related