Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author 068724947
- */
- public class SongDatabase {
- public static void main(String[] args) {
- // creates a new arraylist for the mp3
- ArrayList<MP3> al = new ArrayList<MP3>();
- String title = null, artist = null, year = null, searchTerm;
- Scanner s = null;
- int counter = 0;
- // reads in the file
- try {
- s = new Scanner(new File("MP3List.csv"));
- } catch (FileNotFoundException ex) {
- System.out.println("Error: file not found.");
- }
- s.useDelimiter(",");
- // reads in the mp3 information and adds it to the arraylist
- while (s.hasNextLine()) {
- if (s.hasNext()) {
- title = s.next();
- counter++;
- }
- if (s.hasNext()) {
- artist = s.next();
- counter++;
- }
- if (s.hasNext()) {
- year = s.next();
- counter++;
- }
- if (counter == 3) {
- al.add(new MP3(title, artist, year));
- counter = 0;
- }
- Collections.sort(al);
- }
- s.close();
- s = new Scanner(System.in);
- // searches for a string
- while (true) {
- System.out.print("Please enter a search string ('quit' to exit): ");
- searchTerm = s.next();
- if (searchTerm.equals("quit")) {
- break;
- }
- // finds the matching terms
- for (int i = 0; i < al.size(); i++){
- if (al.get(i).compareTo(searchTerm) == 0){
- System.out.println ("Found: " + al.get(i));
- }
- }
- }
- System.out.println ("Goodbye!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement