0

So I have a class called MusicSelection and I must create 2 ArrayLists of MusicSelection with 5 string parameters. I have the list of 100 strings from a .txt file stored in play. I need to create 20 MusicSelection objects using these 100 strings and store them in lib. But I am having the hardest time figuring out how to do this. Please help. Thank you!

public class MusicSelection {

String g;
String a;
String t;
String al;
String d;    

public MusicSelection (String genre, String artist, String title, String album, String date){
    g = genre;
    a = artist;
    t = title;
    al = album;
    d = date;
}
public static void main(String[] args) {
    Scanner play = null;
    int choice = 0;
    Scanner input = new Scanner(System.in);
    ArrayList<MusicSelection> lib = new ArrayList();
    ArrayList<MusicSelection> plist = new ArrayList();

    System.out.println("Welcome to the playlist creator!");
    while(choice!=5){
        System.out.println("Please make a choice");
        System.out.println("1: Create playlist");
        System.out.println("2: Delete playlist");
        System.out.println("3: Add selection to playlist");
        System.out.println("4: Remove seletion from playlist");
        System.out.println("5: Quit");
        choice = input.nextInt();
        if (choice==1&&play==null){
            try
            {
               play =new Scanner(new FileInputStream("lisst.txt"));
            }
            catch(FileNotFoundException e)
            {
               System.out.println("Problem opening files.");
               System.exit(0);
            }
            while(play.hasNextLine()){

            }
1
  • What, specifically, are you having trouble with? You seem to be just asking StackOverflow to do your homework for you. While we could do that, it doesn't seem like it would help you. Ask a specific question about the part you're having trouble with. If you do not yet have such a question yet, study more. Commented Mar 5, 2016 at 4:38

1 Answer 1

1

Assuming your file looks something like this:

Progressive Rock,Pink Floyd,Time,The Dark Side of the Moon,1974
Progressive Rock,Pink Floyd,On the Run,The Dark Side of the Moon,1974

package fileio;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;

public class ReadingFiles {
String g;
String a;
String t;
String al;
String d;
ArrayList<ReadingFiles> pList = new ArrayList<ReadingFiles>();

public ReadingFiles(){

}

public ReadingFiles(String[] ar){
    this(ar[0], ar[1], ar[2], ar[3], ar[4]);
}

public ReadingFiles (String genre, String artist, String title, String album, String date){
    g = genre;
    a = artist;
    t = title;
    al = album;
    d = date;
}

public void addData(ReadingFiles file){
    pList.add(file);
}

public void deserializeFile(){
    try{
        BufferedReader br = new BufferedReader(new FileReader("/Users/droy/var/musicplist.txt"));
        String line = null; 
        while ((line = br.readLine()) != null) {
            String ar[] = line.split(",");
            addData(new ReadingFiles(ar));
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
}


public static void main(String[] args) {
    ReadingFiles file = new ReadingFiles();
    file.deserializeFile();
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

But now that I have the array list of strings, how do I put them into a musicSelection arraylist in increments of 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.