Results 1 to 4 of 4
Thread: Help with arrays and array lists
- 12-04-2009, 05:30 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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.
Another class, CDList, which is the array list part: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; }
And the tester,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; }
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.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")); } }
Can I even write my do loop like I did in my tester class?
Any help would be massively appreciated. Thanks!
- 12-04-2009, 02:47 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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?
- 12-04-2009, 04:21 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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
- 12-04-2009, 09:47 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
null point exception in array lists
By c_walker in forum New To JavaReplies: 3Last Post: 10-17-2009, 05:38 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM -
[SOLVED] Creating an Array of Arrays?
By xcallmejudasx in forum Advanced JavaReplies: 5Last Post: 11-04-2008, 06:01 PM -
Stacks, lists...
By little_polarbear in forum New To JavaReplies: 7Last Post: 08-02-2008, 01:59 PM -
please i need the code of comparing these two array lists.
By raj reddy in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 04-18-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks