CSV-Datei per Scanner eingelesen und in Song-Array gespeichert

main
Giorgi Vardanashvili 2026-11-15 20:38:50 +01:00
parent 08794f41c6
commit c5ee072b4e
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
import java.io.File;
import java.util.Scanner;
public class PlaylistAnalyse {
public static void main(String[] args) throws Exception {
String dateipfad = "playlist.csv";
Song[] songs = ladeSongsAusDatei(dateipfad);
System.out.println("Erfolgreich geladene Songs: " + songs.length);
}
public static int zaehleZeilen(String dateipfad) throws Exception {
Scanner sc = new Scanner(new File(dateipfad), "UTF-8");
int zeilen = 0;
while (sc.hasNextLine()) {
sc.nextLine();
zeilen++;
}
sc.close();
return zeilen;
}
public static Song[] ladeSongsAusDatei(String dateipfad) throws Exception {
int gesamtZeilen = zaehleZeilen(dateipfad);
Song[] songs = new Song[gesamtZeilen - 1];
Scanner sc = new Scanner(new File(dateipfad), "UTF-8");
if (sc.hasNextLine()) {
sc.nextLine();
}
int index = 0;
while (sc.hasNextLine()) {
String zeile = sc.nextLine();
String[] spalten = zeile.split(";");
String titel = spalten[0];
String kuenstler = spalten[1];
String album = spalten[2];
int dauer = Integer.parseInt(spalten[3]);
int jahr = Integer.parseInt(spalten[4]);
int pop = Integer.parseInt(spalten[5]);
songs[index] = new Song(titel, kuenstler, album, dauer, jahr, pop);
index++;
}
sc.close();
return songs;
}
}