3

I have a class called CD with the following private variables:

private String artist = "";
private String year = "";
private String albumName = "";
private ArrayList<String> songs = new ArrayList<String>();

This class is used to store input data that is in this format:

Led Zeppelin
1979 In Through the Outdoor
-In the Evening
-South Bound Saurez
-Fool in the Rain
-Hot Dog
-Carouselambra
-All My Love
-I'm Gonna Crawl

I have a CDParser class that is in charge of parsing the file called sample.db line by line to store it into our CD object. After parsing, the CD object, after initializing it with CD newCD = new CD() has the following structure:

artist = "Led Zeppelin"

year = "1979"

albumName = "In Through the Outdoor"

songs = {"-In the Evening", "-South Bound Saurez", "-Fool in the Rain", "-Hot Dog"}

Now.. For this project, sample.db contains many albums, which looks like the following:

Led Zeppelin
1979 In Through the Outdoor
-In the Evening
-South Bound Saurez
-Fool in the Rain
-Hot Dog
-Carouselambra
-All My Love
-I'm Gonna Crawl

Led Zeppelin
1969 II
-Whole Lotta Love
-What Is and What Should Never Be
-The Lemon Song
-Thank You
-Heartbreaker
-Living Loving Maid (She's Just a Woman)
-Ramble On
-Moby Dick
-Bring It on Home

Bob Dylan
1966 Blonde on Blonde
-Rainy Day Women #12 & 35
-Pledging My Time
-Visions of Johanna
-One of Us Must Know (Sooner or Later)
-I Want You
-Stuck Inside of Mobile with the Memphis Blues Again
-Leopard-Skin Pill-Box Hat
-Just Like a Woman
-Most Likely You Go Your Way (And I'll Go Mine)
-Temporary Like Achilles
-Absolutely Sweet Marie
-4th Time Around
-Obviously 5 Believers
-Sad Eyed Lady of the Lowlands

I have so far been able to parse all three different albums and save them into my CD object, but ran into a roadblock where I'm simply saving all three albums into the same newCD object.

My question is - is there a way of programmatically initialize my CD constructor that will follow the format newCD1, newCD2, newCD3, etc, as I parse the sample.db?

What this means is, as I parse this particular file:

  1. newCD1 would be the album In Through the Outdoor (and its respective private vars)

  2. newCD2 would be the album II (and its respective private vars)

  3. newCD3 would be the album Blonde on Blonde, and so on

Is this a smart way to do it? Or could you suggest me a better way?

EDIT:

Attached is my parser code. ourDB is an ArrayList containing every line of sample.db:

    CD newCD = new CD();

    int line = 0;

    for(String string : this.ourDB) {
        if(line == ARTIST) {
            newCD.setArtist(string);
            System.out.println(string);
            line++;
        } else if(line == YEAR_AND_ALBUM_NAME){
            String[] elements = string.split(" ");

            String[] albumNameArr = Arrays.copyOfRange(elements, 1, elements.length);

            String year = elements[0];
            String albumName = join(albumNameArr, " ");

            newCD.setYear(year);
            newCD.setAlbumName(albumName);

            System.out.println(year);
            System.out.println(albumName);

            line++;
        } else if(line >= SONGS && !string.equals("")) {
            newCD.setSong(string);
            System.out.println(string);
            line++;
        } else if(string.isEmpty()){
            line = 0;
        }
    }
5
  • its not clear what you are trying to do. also if possible add current code snippet Commented Jun 4, 2015 at 20:24
  • Seems like you not looping correctly between objects. Show us more code you have. Commented Jun 4, 2015 at 20:25
  • Are you looking for arrays and lists? Commented Jun 4, 2015 at 20:25
  • I would steer you away from trying to do this through constructors. Instead, just use a POJO in which you can add more albums & songs as needed Commented Jun 4, 2015 at 20:26
  • You already have a List inside your CD class that you are using. Use one outside too, to store your CDs. Otherwise use a Map to index the CDs Commented Jun 4, 2015 at 20:26

3 Answers 3

7

You have a single CD object, so you keep overwriting it. Instead, You could hold a collection of CDs. E.g.:

List<CD> cds = new ArrayList<>();

CD newCD = new CD();
int line = 0;

for(String string : this.ourDB) {
    if(line == ARTIST) {
        newCD.setArtist(string);
        System.out.println(string);
        line++;
    } else if(line == YEAR_AND_ALBUM_NAME){
        String[] elements = string.split(" ");

        String[] albumNameArr = Arrays.copyOfRange(elements, 1, elements.length);

        String year = elements[0];
        String albumName = join(albumNameArr, " ");

        newCD.setYear(year);
        newCD.setAlbumName(albumName);

        System.out.println(year);
        System.out.println(albumName);

        line++;
    } else if(line >= SONGS && !string.equals("")) {
        newCD.setSong(string);
        System.out.println(string);
        line++;
    } else if(string.isEmpty()){
        // We're starting a new CD!
        // Add the one we have so far to the list, and start afresh
        cds.add(newCD);
        newCD = new CD();
        line = 0;
    }
}

// Take care of the case the file doesn't end with a newline:
if (line != 0) {
    cds.add(newCD);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes! This is what I mean!
Ah.. My source of confusion came from the fact that I thought each instance I store within my collection needs to have a different name, i.e. if I have a newCD within my list of newCDs, the next instance must not contain the same name.
Hi Mureinik. With this, I am running into the problem where I am only printing out the first two albums - the third album seems to be missing. I went ahead to added print statements within each if statement before the final else-if, and I was getting the correct outputs, but if I try my CD object at the string.isEmpty() if-statement, I only get two CD objects.
@theGreenCabbage I assumed, for some reason, you'd have an empty line at the end of the file. See my edited answer.
5

The problem is that you're using the same object reference of CD to fill the values of the parse of the file.

Just make sure to initialize and store every instance of CD newCD every time you start parsing the content of a new album.

You may do the following:

List<CD> cdList = new ArrayList<>();
for (<some way to handle you're reading a new album entry from your file>) {
    CD cd = new CD();
    //method below parses the data in the db per album entry
    //an album entry may contain several lines
    parseData(cd, this.ourDB);
    cdList.add(cd);
}
System.out.println(cdList);

Your current way to parse the file works but is not as readable as it should be. I would recommend using two loops:

List<CD> cdList = new ArrayList<>();
Iterator<String> yourDBIterator = this.ourDB.iterator();
//it will force to enter the first time
while (yourDBIterator.hasNext()) {
    //do the parsing here...
    CD cd = new CD();
    //method below parses the data in the db per album entry
    //an album entry may contain several lines
    parseData(cd, yourDBIterator);
    cdList.add(cd);
}

//...
public void parseData(CD cd, Iterator<String> it) {
    String string = it.next();
    int line = ARTIST;
    while (!"".equals(string)) {
        if (line == ARTIST) {
            newCD.setArtist(string);
            System.out.println(string);
            line++;
        } else if(line == YEAR_AND_ALBUM_NAME){
            String[] elements = string.split(" ");
            String[] albumNameArr = Arrays.copyOfRange(elements, 1, elements.length);
            String year = elements[0];
            String albumName = join(albumNameArr, " ");
            newCD.setYear(year);
            newCD.setAlbumName(albumName);
            System.out.println(year);
            System.out.println(albumName);
            line++;
        } else if(line >= SONGS && !string.equals("")) {
            newCD.setSong(string);
            System.out.println(string);
            line++;
        }
        if (it.hasNext()) {
            string = it.next();
        } else {
            string = "";
        }
    }
}

Then, your code

3 Comments

I am indeed using the same instance of the CD class. Which is my question - how do I dynamically generate a new instance to store each album?
@theGreenCabbage just do CD newCD = new CD(); properly.
Thank you - this is precisely what I meant
0

I suggest to use the Builder design pattern to construct the CD object. If you read lines always in the same order, it will be not complicated to implement and use. Good tutorial: http://www.javacodegeeks.com/2013/01/the-builder-pattern-in-practice.html

Comments

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.