-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVHelper.java
55 lines (48 loc) · 1.79 KB
/
CSVHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.mycompany.csc365p1;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVHelper {
List<List<String>> songs = new ArrayList<>();
public CSVHelper() {
try {
BufferedReader br = new BufferedReader(new FileReader("Songs Dataset.csv"));
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
values = Arrays.stream(values).map(value -> value.replaceAll(",", "")).toArray(String[]::new);
songs.add(Arrays.asList(values));
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void importSongs() {
try {
for (List<String> info: songs.subList(1, songs.size())) {
String title = info.get(3);
String artist = info.get(1);
String album = info.get(6);
String genre = info.get(5);
String era = info.get(4);
int duration = (int)Math.ceil(Double.parseDouble(info.get(2)));
System.out.println("Title: " + title
+ ", Arist: " + artist
+ ", Album: " + album
+ ", Genre: " + genre
+ ", Era: " + era
+ ", Duration: " + duration
);
App.dbConn.insertSong(new Song(title, artist, album, duration, genre, era));
}
System.out.println("Inserted " + songs.size() + " songs");
} catch (SQLException e) {
e.printStackTrace();
}
}
}