-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemixerFiller.java
More file actions
90 lines (72 loc) · 2.78 KB
/
Copy pathRemixerFiller.java
File metadata and controls
90 lines (72 loc) · 2.78 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.example.RemixerFiller;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class RemixerFiller {
Scanner scanner = new Scanner(System.in);
private String[] lyrics;
private String fileName = "lyrics.txt"; // this should match your txt file
private String[] wordsToReplace; // for part 2, if you finish the main project
private String[] newWords;
private int numOfwords;
public void remix() {
numOfwords = scanner.nextInt();
wordsToReplace = new String[numOfwords];
newWords = new String[numOfwords];
for (int i = 0; i <= numOfwords-1; i++){
wordsToReplace[i] = scanner.next();
}
for (int i = 0; i <= numOfwords-1; i++){
newWords[i] = scanner.next();
}
for (int i = 0; i <= lyrics.length - 1; i++){
for (int j = 0; j <= numOfwords-1; j++){
if (lyrics[i].equals(wordsToReplace[j])){
lyrics[i] = newWords[j];
}
}
}
}
// DON'T TOUCH THE BELOW CODE //
public RemixerFiller() {
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
ArrayList<String> tempLyrics = new ArrayList<String>();
for (String line = in.readLine(); line != null; line = in.readLine()) {
String[] words = line.split(" ");
for (String w : words) {
tempLyrics.add(w.toLowerCase().replaceAll("^[^a-zA-Z0-9\\s]+|[^a-zA-Z0-9\\s]+$", ""));
}
tempLyrics.add("\n");
}
in.close();
lyrics = new String[tempLyrics.size()];
for (int i = 0; i < tempLyrics.size(); i++) {
lyrics[i] = tempLyrics.get(i);
}
remix();
if (fileName.substring(fileName.length()-4).equals(".txt")) {
fileName = fileName.substring(0, fileName.length()-4);
}
BufferedWriter out = new BufferedWriter(new FileWriter(fileName + "_remixed"));
for (String s : lyrics) {
out.write(s + (s.equals("\n") ? "" : " "));
}
out.close();
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new RemixerFiller();
}
}