From c5ee072b4eb0608bac6a03654f711de81a5a91ef Mon Sep 17 00:00:00 2001 From: Giorgi Vardanashvili Date: Sun, 15 Nov 2026 20:38:50 +0100 Subject: [PATCH] CSV-Datei per Scanner eingelesen und in Song-Array gespeichert --- src/PlaylistAnalyse.java | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/PlaylistAnalyse.java diff --git a/src/PlaylistAnalyse.java b/src/PlaylistAnalyse.java new file mode 100644 index 0000000..0189efc --- /dev/null +++ b/src/PlaylistAnalyse.java @@ -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; + } +} \ No newline at end of file