1

I'm having trouble figuring out how to properly do this. So I have this array that can contain 6 items. I have this .txt file that contains a song on a line, and then on the next who the song is by, and then the next song, and then who that's by. So on and so forth for a few. In total there are 12 lines in my .txt file, but I can only put a total of 6 items in my array. So I'm wondering, how can I put both the title of the song + the artist into a single index on the array. Such that I can print it out later as the Title "by" Artist.

My code is decently short, so maybe viewing it might help.

/**This program creates a list of songs for a CD by reading from a file*/
import java.io.*;

public class CompactDisc
{
    public static void main(String [] args) throws IOException
    {
    FileReader file = new FileReader("Classics.txt");
    BufferedReader input = new BufferedReader(file);
    String title;
    String artist;

    //Declare an array of songs, called cd, of size 6
    String[] cd = new String[6];

    for (int i = 0; i < cd.length; i++)
    {
        title = input.readLine();
        artist = input.readLine();  
        // fill the array by creating a new song with 
        // the title and artist and storing it in the 
        // appropriate position in the array
     cd.add(title + artist);
    }

    System.out.println("Contents of Classics:");
    for (int i = 0; i < cd.length; i++)
    {
        //print the contents of the array to the console
    }
  }
}

This is what's in the .txt file

Ode to Joy
Bach
The Sleeping Beauty
Tchaikovsky
Lullaby
Brahms
Canon
Bach
Symphony No. 5
Beethoven
The Blue Danube Waltz
Strauss

The final output is supposed to print as:

Contents of Classics
Ode to Joy by Bach
The Sleeping Beauty by Tchaikovsky
Lullaby by Brahms
Canon by Bach
Symphony No. 5 by Beethoven
The Blue Danube Waltz by Strauss
1
  • You can concatenate the title and artist to a single string (String result = title + "||" + artist;) with a character or string you choose, place it into the array. And to split the string in the display loop, use String#split. Of course, this doesn't take into account any errors that could come from malformed files and such. Commented Mar 10, 2014 at 0:41

2 Answers 2

2

You can just replace:

cd.add(title + artist);

by:

cd[i] = title + " by " + artist;

And just print it with:

System.out.println(cd[i]);
Sign up to request clarification or add additional context in comments.

4 Comments

I get: "error: cannot find symbol" pointing at the period in the line.
@WillBro Hmmm, yeah, I just took your code and assumed it would compile. I just edited the answer.
Your edit worked. I'm going to have to look into why cd.add wouldn't work but cd[i] did. I guess that's something to do tomorrow. Thank you.
@WillBro cd.add() would work if you were using an ArrayList instead of an array.
0

I would go with @dasblinkenlight's solution, however I would add a toString method to the Track class.

I would also go further and make it an immutable value type object, but then thats probably over the top for your needs.

The toString would allow you to do:

System.out.println("Track: " + track);

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.