• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Reading csv file

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have an assignment to read student grades from a csv file.  Each represents a result a certain student obtained for a certain course.  
A student can appear on several lines, since he/she has taken a number of courses.  

The Student class constructor looks like this



So a student has a list of results.

For the readCsv () function my guess would be to make a list of maps.  The student being the key and the result arraylist as the value(s).



I keep getting on an error on the way the studentlist is composed.
Would it be better to just store in a list of strings and then use separate logic to get the results student by student ?
How would I be able to recognize identical students ?  Surname and name are in different columns.

Thanks in advance.
 
Marshal
Posts: 81610
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

In most real‑life applications one wouldn't read a CSV directly, but use a program designed to cope with CSVs. Obviously your assignment forces you to read the file “by hand”. How you do that depends on the format of the file and what delimiters you are using.
I would suggest you initialise the List in the constructor. You cannot be sure that a setXXX() method will ever be called and your object will be created in an inconsistent state. I would also have used a Scanner to parse the lines. Depending on the delimiter, you can use the Scanner object to read the whole file or read each line separately.You don't need the counter. If you have a line with headers in, you can simply read one line before the start of the loop. You don't need the arrays; add the elements to the List directly.
Your Student object should take a defensive copy of the List, otherwise it is possible for its state to be changed by outside code without your knowing about it. Strings, being immutable, do not need to be copied.
Why have you got all those setXXX() methods? I know many people think they are an essential part of a class, but that isn't a part of the general tenets of object‑orientation. There are great advantages to making an object immutable.
 
Bartender
Posts: 5749
216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Andreas,

welcome to the Ranch and enjoy the stay!

Your studentList is a List of Maps, so

will not work. Instead, you should do something like:

or

But a Student already has a field results. being a List<Result>. So why create that Map<Student, ArrayList<Result> ?


Edit: I am a bit confused about Result and an ArrayList<Result>. What does a Result represent?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an assignment like this it may be fine, but in general you shouldn't try to handle CSV using string operations, but use a library like Apache Commons CSV instead. CSV is not as simple a file format as it looks at first, and before you're done coding all its special cases, you would be long done using a library.
 
Andreas Loon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and thanks for the swift reply.
The csv is in the following format :

email surname name course grade type Weight Period
[email protected]Jef Daniels Operating Systems 16.33 PartialEvaluation 1 25% november
[email protected]Jef Daniels Hardware xx PartialEvaluation 1 50% november
[email protected]Jef Daniels Databank 9.0 PartialEvaluation 1 50% november
[email protected]Kenny Rogers Hardware 8.66 PartialEvaluation 1 50% november
[email protected]Kenny Rogers Operating Systems 12.33 PartialEvaluation 1 25% november
[email protected]Kenny Rogers OO Concepts 7.00 PartialEvaluation 1 30% november
[email protected]Greta Thunberg OO Concepts 11.00 PartialEvaluation 1 30% november
[email protected]Greta Thunberg Hardware 15.33 PartialEvaluation 1 50% november
[email protected]Greta Thunberg Operating Systems 17.33 PartialEvaluation 1 25% november

The goal not to just show information, an assessment needs to be made if a student for example fails at more then 4 courses, he receives an email with a certain message, same goes for passing 3 or more courses etc. all with different types of feedback emails.
So in my view for a student object needs to have a set of results in order to evaluate him.   I think I just best look into openCsv or Apache csv.
 
Campbell Ritchie
Marshal
Posts: 81610
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas Loon wrote:. . . thanks . . .

That's a pleasure

The csv is in the following format :

email surname name course grade type Weight Period
[email protected]Jef Daniels Operating Systems 16.33 PartialEvaluation 1 25% november
[email protected]Jef Daniels Hardware xx PartialEvaluation 1 50% november
[email protected]Jef Daniels Databank 9.0 PartialEvaluation 1 50% november
[email protected]Kenny Rogers Hardware 8.66 PartialEvaluation 1 50% november
[email protected]Kenny Rogers Operating Systems 12.33 PartialEvaluation 1 25% november
[email protected]Kenny Rogers OO Concepts 7.00 PartialEvaluation 1 30% november
[email protected]Greta Thunberg OO Concepts 11.00 PartialEvaluation 1 30% november
[email protected]Greta Thunberg Hardware 15.33 PartialEvaluation 1 50% november
[email protected]Greta Thunberg Operating Systems 17.33 PartialEvaluation 1 25% november
. . .

That isn't a CSV file. It is tab‑separated, which is rather similar to CSV. You can parse it with "\\t" as a delimiter, but that probably won't pick up “november” which is terminated by a line end. You can correct that with a combination of reading lines and reading individual tokens later.
You will have to find some way to convert 25% to 0.25.
You have got name and surname in the wrong order.
You will have to find some way to record some sort of ID (maybe using email as a key) against the Student object. Maybe methods like Map#putIfAbsent() wll be useful there. You will then need a method to add a score. The scores ending ….33 and ….66 won't add up correctly.
You will fail to read Jef Daniels' second line because of the, “xx.” If I found that in a file, I would return the file to its creator as “format corrupted”.
 
Piet Souris
Bartender
Posts: 5749
216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you override the equals-method in your Student class (and don't forget the hashCode method!), and you create a student and a result from each line, then you can do:

and you can retrieve information for example:
 
Andreas Loon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far this works



To deal with the results, I created the following Result class



The getter to retrieve grades



To get overall results a separate PeriodResult class is made with an enum to indicate the colour code depending on the amount of failed or passed courses





The main method



As a final result I need to make a GUI to load and represent the data and send emails to the students.
My idea of the gui was the attached setup.  Fxml is not allowed, the idea was to implement it as a tableview.

How would I load the csv read data into a tableview ?





Tableview.png
[Thumbnail for Tableview.png]
 
Maybe he went home and went to bed. And took this tiny ad with him:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic