How to read from a .csv file in Java?

Joined
Nov 5, 2023
Messages
2
Reaction score
1
I have a class School, that extends in class Students and teachers. In general, they both have a name, a date of birth and an amount of money per month. Students have class number and amount of lessons, and degree and the subject they teach. How can I read them from .csv file and put into a List of School?

csv file:
Michael;1998-06-21;200;student,9;9
John;2002-07-04;2400;profesor;1
 
Joined
Nov 6, 2023
Messages
4
Reaction score
0
public class MainApp {
public static void main(String[] args) throws FileNotFoundException {
// DateTimeFormatter formatter= new DateTimeFormatterBuilder().appendPattern("YYYY-MM-dd").toFormatter();

List<Scoala> scoala = new ArrayList<>();
BufferedReader fisier = new BufferedReader(new FileReader("src/fisier.txt"));

try {
String linie;
String[] array;
while ((linie = fisier.readLine()) != null) {
array = linie.split(";");
String nume = array[0];
LocalDate data_n = LocalDate.parse(array[1]);//formatter);
float bani = Float.parseFloat(array[2]);
String tip = array[3];
if (tip.equals("elev")) {
float medie = Float.parseFloat(array[4]);
int nr_ob = Integer.parseInt(array[5]);
String clasa = array[6];
Statut statut;
switch (array[7].toLowerCase()) {
case "eminent":
statut = Statut.eminent;
break;
case "fruntas":
statut = Statut.fruntas;
break;
default:
statut = Statut.eminent;
break;
}
Elevi student = new Elevi(nume, data_n, bani, medie, nr_ob, clasa, statut);
scoala.add(student);
}else{
int grad=Integer.parseInt(array[4]);
String materie=array[5];
Profi profesor=new Profi(nume, data_n, bani, grad, materie);
scoala.add(profesor);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}

for(Scoala s: scoala){
System.out.println(s);

}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,607
Messages
2,568,992
Members
44,089
Latest member
Yazidi

Latest Threads

Top