Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class MuddledMusic{
- //In order to keep track of each song, we make a song class that has the song name and author, so we dont have to have the program keep control of that
- public static class Song{
- //Data that will be stored
- public String name;
- public String author;
- //Constructor
- public Song(String name, String author){
- this.name = name;
- this.author = author;
- }
- //Return author without the "The "
- public String effectiveAuthor(){
- return author.replace("The ", "");
- }
- //Return what it would look like in the input file(EX: "Hello - Adele")
- public String toString(){
- return name + " - " + author;
- }
- }
- public static class SongComparator implements Comparator<Song>{
- public int compare(Song a, Song b){
- return a.effectiveAuthor().compareTo(b.effectiveAuthor());
- }
- }
- public static void printArray(Song[] in){
- for(Song n : in){
- System.out.println(n.toString());
- }
- }
- public static void main(String[] args) throws FileNotFoundException,IOException{
- //String to input file
- String filePath = "Prob08.in.txt";
- //New buffered reader of file reader
- BufferedReader br = new BufferedReader(new FileReader(filePath));
- //Grab test cases
- String testCasesStr = br.readLine();
- //Parse to int
- int testCases = Integer.parseInt(testCasesStr);
- //loop through test cases
- for(int i = 0; i < testCases; i++){
- //Grab song-artist pairs
- String songArtistPairsStr = br.readLine();
- //Parse to int
- int songArtistPairs = Integer.parseInt(songArtistPairsStr);
- //Make an array of Song objects with the length of the amount of pairs
- Song[] pairsOrig = new Song[songArtistPairs];
- //loop through the pairs
- for(int j = 0; j < songArtistPairs; j++){
- //Read line
- String line = br.readLine();
- //Split up line to make it easier to assign
- String[] lineSplit = line.split(" - ");
- //Assign variables
- String song = lineSplit[0];
- String author = lineSplit[1];
- //Create new Song objects
- Song curPair = new Song(song, author);
- //Put into array
- pairsOrig[j] = curPair;
- }
- //Now we have filled up the array of song objects
- //Let's copy that into a new array
- Song[] pairs = new Song[songArtistPairs];
- System.arraycopy(pairsOrig, 0, pairs, 0, pairsOrig.length);
- Arrays.sort(pairs, new SongComparator());
- printArray(pairs);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement