SHARE
TWEET

Untitled

a guest Jul 17th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Scanner;
  7.  
  8. /*
  9.  * To change this template, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12. /**
  13.  *
  14.  * @author 068724947
  15.  */
  16. public class SongDatabase {
  17.  
  18.     public static void main(String[] args) {
  19.         // creates a new arraylist for the mp3
  20.         ArrayList<MP3> al = new ArrayList<MP3>();
  21.         String title = null, artist = null, year = null, searchTerm;
  22.         Scanner s = null;
  23.         int counter = 0;
  24.         // reads in the file
  25.         try {
  26.             s = new Scanner(new File("MP3List.csv"));
  27.         } catch (FileNotFoundException ex) {
  28.             System.out.println("Error: file not found.");
  29.         }
  30.         s.useDelimiter(",");
  31.         // reads in the mp3 information and adds it to the arraylist
  32.         while (s.hasNextLine()) {
  33.             if (s.hasNext()) {
  34.                 title = s.next();
  35.                 counter++;
  36.             }
  37.             if (s.hasNext()) {
  38.                 artist = s.next();
  39.                 counter++;
  40.             }
  41.             if (s.hasNext()) {
  42.                 year = s.next();
  43.                 counter++;
  44.             }
  45.             if (counter == 3) {
  46.                 al.add(new MP3(title, artist, year));
  47.                 counter = 0;
  48.             }
  49.             Collections.sort(al);
  50.         }
  51.         s.close();
  52.         s = new Scanner(System.in);
  53.         // searches for a string
  54.         while (true) {
  55.             System.out.print("Please enter a search string ('quit' to exit): ");
  56.             searchTerm = s.next();
  57.             if (searchTerm.equals("quit")) {
  58.                 break;
  59.             }
  60.             // finds the matching terms
  61.             for (int i = 0; i < al.size(); i++){
  62.                 if (al.get(i).compareTo(searchTerm) == 0){
  63.                     System.out.println ("Found: " + al.get(i));
  64.                 }
  65.             }
  66.         }
  67.         System.out.println ("Goodbye!");
  68.     }
  69. }
RAW Paste Data
Top