Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MuddledMusic{
  5.     //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
  6.     public static class Song{
  7.         //Data that will be stored
  8.         public String name;
  9.         public String author;
  10.         //Constructor
  11.         public Song(String name, String author){
  12.             this.name = name;
  13.             this.author = author;
  14.         }
  15.         //Return author without the "The "
  16.         public String effectiveAuthor(){
  17.             return author.replace("The ", "");
  18.         }
  19.         //Return what it would look like in the input file(EX: "Hello - Adele")
  20.         public String toString(){
  21.             return name + " - " + author;
  22.         }
  23.     }
  24.     public static class SongComparator implements Comparator<Song>{
  25.         public int compare(Song a, Song b){
  26.             return a.effectiveAuthor().compareTo(b.effectiveAuthor());
  27.         }
  28.     }
  29.     public static void printArray(Song[] in){
  30.         for(Song n : in){
  31.             System.out.println(n.toString());
  32.         }
  33.     }
  34.     public static void main(String[] args) throws FileNotFoundException,IOException{
  35.         //String to input file
  36.         String filePath = "Prob08.in.txt";
  37.         //New buffered reader of file reader
  38.         BufferedReader br = new BufferedReader(new FileReader(filePath));
  39.         //Grab test cases
  40.         String testCasesStr = br.readLine();
  41.         //Parse to int
  42.         int testCases = Integer.parseInt(testCasesStr);
  43.         //loop through test cases
  44.         for(int i = 0; i < testCases; i++){
  45.             //Grab song-artist pairs
  46.             String songArtistPairsStr = br.readLine();
  47.             //Parse to int
  48.             int songArtistPairs = Integer.parseInt(songArtistPairsStr);
  49.             //Make an array of Song objects with the length of the amount of pairs
  50.             Song[] pairsOrig = new Song[songArtistPairs];
  51.             //loop through the pairs
  52.             for(int j = 0; j < songArtistPairs; j++){
  53.                 //Read line
  54.                 String line = br.readLine();
  55.                 //Split up line to make it easier to assign
  56.                 String[] lineSplit = line.split(" - ");
  57.                 //Assign variables
  58.                 String song = lineSplit[0];
  59.                 String author = lineSplit[1];
  60.                 //Create new Song objects
  61.                 Song curPair = new Song(song, author);
  62.                 //Put into array
  63.                 pairsOrig[j] = curPair;
  64.             }
  65.             //Now we have filled up the array of song objects
  66.             //Let's copy that into a new array
  67.             Song[] pairs = new Song[songArtistPairs];
  68.             System.arraycopy(pairsOrig, 0, pairs, 0, pairsOrig.length);
  69.             Arrays.sort(pairs, new SongComparator());
  70.             printArray(pairs);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement