There a file of audio snippets in a form described below.
Write a Java program to interact with a user to get a genre from them and produce a file containing all the audio snippets from the file snippets.all that match the chosen genre. The name of the output file should be the name of the chosen genre with the extension .all.
The format of an andio snippet in a file consists of two parts: [ID] and [DATA].
The id part is always [ID] on one line followed by four lines: title, composer, genre, and length (a double representing the number of hours).
The data is always [ID] on one line followed by some number (at least one) of integers not necessarily on separate lines but always separated with whitespace.
The order of [ID] part and [DATA] part is not specified but you are guaranteed that is one is there, the other will also be there.
There is no blank line between audio snippet entries.
Here’s a (small) sample file of audio snippets.
If the user chose “ElectronicCountry” as the genre to filter on, this would be the output file based on that sample file.
HINT: notice that in the output file the data “numbers” are separated by three spaces – they are not on different lines.
HINT: can you read a long as a string?
If there are no matches, the file should be empty.
The order of the snippets in the output file should be the same as the order from the input file.
Genre names have no spaces so that they can be used as filenames on some OSs.
Obviously, you can’t change or wipe out the input file.
Interaction with the user can be done in any of the ways you know to interact with a user.
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class songSnippets {
public static void main(String[] args) throws IOException {
Scanner kbd = new Scanner(System.in);
File file = new File("/snippets.all");
Scanner infl = new Scanner(file);
System.out.print("Enter the name of the genre you want: ");
String genreFromUser = kbd.nextLine();
File fileO = new File(genreFromUser + ".all");
if (fileO.exists()) {
System.out.println("can no do, file exist");
System.exit(1);
}
System.out.println();
System.out.println("Thank you. Lets make your file: " + genreFromUser + ".all");
System.out.println();
PrintWriter outfl = new PrintWriter(fileO);
final String ID = "[ID]";
final String DATA = "[DATA]";
long data;
String temp;
boolean out = true;
String songLength;
while (true) {
out = true;
while (out) {
if (infl.hasNext()){
temp = infl.next();
if (temp.equals(ID)){
infl.nextLine();
out = false;
}
}
else{
System.out.println("All, done. Now I'll clean up...");
infl.close();
outfl.close();
System.exit(1);
}
}
String songName = infl.nextLine();
String songComposer = infl.nextLine();
String songGenre = infl.nextLine();
if (songGenre.equals(genreFromUser)){
songLength = infl.nextLine();
outfl.println("[ID]");
outfl.println(songName);
outfl.println(songComposer);
outfl.println(songGenre);
outfl.println(songLength);
outfl.println("[DATA]");
while (true) {
temp = infl.next();
if (temp.equals(DATA)){
infl.nextLine();
while (infl.hasNextLong()) {
data = infl.nextLong();
outfl.print(data + " ");
}
break;
}
}
outfl.println();
}
else{
continue;
}
}
}
}