Results 1 to 4 of 4
  1. #1
    ambernicole88 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default Help with arrays and array lists

    I'm trying to write a program that allows a user to input an album title and a set number (10) of songs for that album. Then the album is saved in an array list. When the user is finished inputting, the program then prints out all of the songs inputed.

    This is my main class, CD.
    Java Code:
    public class CD
    {
       public CD(String album)
        {
            cdName = album;
            songName = new String[10];
        }
    
        public void addSongs(String title, int location)
        {
    // Add the name of the songs to the Array: songName.
            songName[location] = title;
        }
    
        public void printCD()
        {
    // Print the names of the songs in a CD.
            for ( int i = 0; i <= 9; i++ )
            System.out.println(songName[i]);
    
        }
    
    // Instance fields.
    private String cdName;
    private String[] songName;
    
    }
    Another class, CDList, which is the array list part:
    Java Code:
    import java.util.ArrayList;
    
    public class CDList
    {
       public CDList()
        {
            myCDcollection = new ArrayList<CD>();
        }
    
        public void addCD(CD cdTitle)
        {
    // Add the CDs to an array list.
            myCDcollection.add(cdTitle);
        }
    
        public void printAllCDs()
        {
    // Print the name of all the songs in your CD Collection.
            for (CD album : myCDcollection)
            album.printCD();
        }
    
    // Instance field.
    private ArrayList<CD> myCDcollection;
    }
    And the tester,
    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class CDTester
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            
            String answer;
            int count = 0;
            do 
            {
                System.out.print("What is the title of the CD? ");
                String album = in.next();
                
                CD a = new CD(album);
                CDList b = new CDList();
                
                int number = 0;
                int i = 0;
                for(i=0; i<=9; i++)
                {
                    System.out.print("Song title? ");
                    String name = in.next();
                    number = i;
                    a.addSongs(name, number);
                }
                
                b.addCD(a);
                
                System.out.print("Do you have another CD? ");
                answer = in.next();
                
                if (answer.equalsIgnoreCase("yes"))
                    {
    
                    }
                
                else
                    {
                        System.out.print("Would you like to print all songs? ");
                        String print = in.next();
    
                        if (print.equalsIgnoreCase("yes"))
                            {
                                b.printAllCDs();
                            }
                    }
                    
               count = count + 1;                
            }
            while(answer.equalsIgnoreCase("yes"));
            
    
        }
        
    }
    It is only printing out the songs from the last album inputed. I think the problem is in my tester, but I'm pretty new to arrays and array lists, and it's starting to jumble together.

    Can I even write my do loop like I did in my tester class?

    Any help would be massively appreciated. Thanks!

  2. #2
    ambernicole88 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    So I remembered that ArrayLists store references and not information, so it that my problem? Do I need to have a loop that assigns new variable names? Is that even possible?

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Each iteration of the loop, you are creating a new List. Try to take out the do-while loop the line that creates the list: new CDList

  4. #4
    ambernicole88 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    You have no idea how much I appreciate you looking this over. It fixed it (of course), so thank you :)

Similar Threads

  1. null point exception in array lists
    By c_walker in forum New To Java
    Replies: 3
    Last Post: 10-17-2009, 06:38 AM
  2. Replies: 21
    Last Post: 11-05-2008, 03:31 PM
  3. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 07:01 PM
  4. Stacks, lists...
    By little_polarbear in forum New To Java
    Replies: 7
    Last Post: 08-02-2008, 02:59 PM
  5. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 08:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •