-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicBest.java
76 lines (58 loc) · 1.59 KB
/
MusicBest.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sjsu.nguyen.cs146.project1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
public class MusicBest {
// Shuffles the playlist
public static void shuffle(String[] array) {
int n = array.length;
Random r = new Random();
r.setSeed(20);
for (int i = n - 1; i > 0; i--) {
// returns range 0 to i-1
int j = r.nextInt(i);
// Swaps
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
public static void main(String[] args) throws IOException {
// Unshuffled Songs
BufferedReader br1 = new BufferedReader(new FileReader("Playlist.txt"));
// For initial size
int lineNum = 1;
while (br1.readLine() != null) {
lineNum++;
}
br1.close();
BufferedReader br2 = new BufferedReader(new FileReader("Playlist.txt"));
// Creates the playlist in Array
String[] a = new String[lineNum];
int count = 0;
while ((a[count] = br2.readLine()) != null) {
count++;
}
br2.close();
a = Arrays.copyOf(a, a.length - 1);
// Print each songs
for (int i = 0; i < a.length; i++) {
// System.out.println(i+1 + ": " + a[i]);
}
// Shuffles the playlist
shuffle(a);
// Print shuffled playlist
for (int i = 0; i < a.length; i++) {
System.out.println(i + 1 + ": " + a[i]);
}
// Creates the new playlist
PrintWriter pw = new PrintWriter("NguyenNhatPlaylist.txt");
for (int i = 0; i < a.length; i++) {
pw.println(a[i]);
}
pw.close();
}
}