In: Computer Science
Module 1 Program
Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for the line. Treat the first line so that it has a timestamp of 0.00. The last line of the lyrics will have a timestamp. All the timestamps will be in double format. If a line has more than one timestamp, use the first timestamp found searching left to right for the outpt timestamp of that line. Some lines may not contain an embedded timestamp. Interpolate the timestamp so that it is evenly spaced from the preceding line that has a timestamp to next line which also has a timestamp. When displaying the output, display the timestamp for the line as double with 2 places past the decimal place. For example if lyrics.txt contains
Ay Fonsi 5.02 DY Oh, oh no, oh no Oh, yeah Diridiri, dirididi Daddy Go Si, sabes que ya llevo un rato mirandote Tengo que bailar 10.4 contigo hoy (DY) Vi que tu 12 mirada 12.1 ya estaba llamandome Muestrame el camino que yo voy (oh) Tu, tu eres el iman y yo soy el metal Me voy acercando y 15.5 voy armando el plan
Then your program should print to the screen
0.00 Ay 2.51 Fonsi 5.02 DY 5.92 Oh, oh no, oh no 6.81 Oh, yeah 7.71 Diridiri, dirididi Daddy 8.61 Go 9.50 Si, sabes que ya llevo un rato mirandote 10.40 Tengo que bailar contigo hoy (DY) 12.00 Vi que tu mirada ya estaba llamandome 13.17 Muestrame el camino que yo voy (oh) 14.33 Tu, tu eres el iman y yo soy el metal 15.50 Me voy acercando y voy armando el plan
A little starter code:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * Program that reads lyrics from file lyrics.txt and prints the lyrics * with a timestamp to the left of each line public class Module1Program { public static final int NUM_LINES = 200; public static void main(String[] args) { File theSong = new File("lyrics.txt"); Scanner input; try { input = new Scanner(theSong); } catch (FileNotFoundException e) { System.out.println("File not found"); return; } while (input.hasNextLine()) { System.out.println(input.nextLine()); } input.close(); } }
Make sure that your program does not have a Package.
Testing: You should test your program in a variety of situations – missing file, empty file, file too long, file with only one timestamp, file with multiple timestamps per line.
If you have any doubts, please give me comment...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Program that reads lyrics from file lyrics.txt and prints the lyrics with a
* timestamp to the left of each line
*/
public class Module1Program {
public static final int NUM_LINES = 200;
public static void main(String[] args) {
File theSong = new File("lyrics.txt");
Scanner input;
try {
input = new Scanner(theSong);
} catch (FileNotFoundException e) {
System.out.println("File not found");
return;
}
if(!input.hasNextLine()){
System.out.println("Empty file");
return;
}
String lines = "";
double prev_time, end_time, diff, time;
prev_time = 0.0;
time = 0.0;
int n = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
String tokens[] = line.split(" ");
// System.out.println(tokens.length);
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].matches("[0-9]+(\\.[0-9]*)?")) {
end_time = Double.parseDouble(tokens[i]);
diff = (end_time - prev_time) / n;
time = prev_time;
// lines += " ";
for (int j = i; j < tokens.length; j++) {
if (!tokens[j].matches("[0-9]+(\\.[0-9]*)?")){
// if(input.hasNextLine())
lines += tokens[j];
}
}
// System.out.println(lines);
for (String str : lines.split("\n")) {
if(!str.isEmpty())
System.out.printf("%.2f %s\n", time, str.trim());
time += diff;
}
time -= diff;
// System.out.println("--------------------");
n = 0;
lines = "";
prev_time = end_time;
break;
// System.out.println(tokens[i]);
} else
lines += tokens[i] + " ";
}
lines += "\n";
n++;
}
// System.out.printf("%.2f %s\n", time, lines);
input.close();
}
}