ArrayList problem ....

I am going to construct a Simple Muise Qrganizer Application.I want to add the Muise that i have to my application ..However, i can`t display it out properly...i think there is an problem on my arrayList..could anyone give me some suggestion...thc ..i have three class : MusicCd,

MusicCdStore, and MusicCdStoreEngine...MusicCd is for a CD data,

MusicCdStore is where i introduce the the arrayList, and MusicCdStoreEngine is the main function..

import java.util.*;

publicclass MusicCdStoreEngine{

publicstaticvoid main(String[] args)

{

boolean quit =false;

readOperation theRo =new readOperation();

MusicCdStore mcs =new MusicCdStore();

finalint SENTINEL = -1;

String sentinel ="quit";

int inputValue = 0;

String muiseCdsTitle;

int muiseCdsYearOfRelease;

while(inputValue != SENTINEL)

{

//inserting

muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");

muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");

mcs =new MusicCdStore(muiseCdsTitle, muiseCdsYearOfRelease);

inputValue = theRo.readInt("Do you want to display the Cd? press -1 to Display or other value to continue...");

}

//System.out.println("Our CD collection is:" );

System.out.println(mcs.toString());

}

//Actually Result: (it is not suppose to have that)

//Please enter your CD`s title : We are the world

//Please enter your CD`s year of release : 1984

//Do you want to display the Cd? press -1 to Display...8

//Please enter your CD`s title : Go go go

//Please enter your CD`s year of release : 1992

//Do you want to display the Cd? press -1 to Display...-1

// Music Cd`s Title: Go go ogoYear of release: 1992

//Expected Result: ( suppose to have that)

//Please enter your CD`s title : We are the world

//Please enter your CD`s year of release : 1984

//Do you want to display the Cd? press -1 to Display...8

//Please enter your CD`s title : Go go go

//Please enter your CD`s year of release : 1992

//Do you want to display the Cd? press -1 to Display...-1

//Music Cd`s Title: We are the world Year of release: 1984 <<--(I can`t this result in Actually Result.

// .it seems that it doesn`t add anything to my arraylist)

// Music Cd`s Title: Go go ogoYear of release: 1992<<--(i only get this result)

//From these two result, it seems that my araylis(in my MusicCdStore) is out of order....could anyone show me what problewm with my code..pls help

}

[code]

import java.util.ArrayList;

publicclass MusicCdStore

{

private ArrayList<MusicCd> MusicCdList;

public MusicCdStore()

{

/*

MusicCdList = new ArrayList<MusicCd>( );

MusicCdList.add(new MusicCd("Jessica"));

MusicCdList.add(new MusicCd("Get to the World"));

MusicCdList.trimToSize();

*/

}

public MusicCdStore(String newMusicCdsTitle,int newYearOfRelease)

{

MusicCdList =new ArrayList<MusicCd>( );

MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));

//MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));

//MusicCdList.add(new MusicCd("Get to the World"));

MusicCdList.trimToSize();

}

public String toString( )

{

String result=" ";

for( MusicCd tempCd : MusicCdList)

{

result += tempCd.toString() +"\n";

}

return result;

}

public ArrayList<MusicCd> searchForTitle(String searchString)

{

ArrayList<MusicCd> searchResult =new ArrayList<MusicCd>();

for(MusicCd currentMusicCd : MusicCdList)

{

if((currentMusicCd.getTitle()).indexOf(searchString) != -1)

searchResult.add(currentMusicCd);

}

searchResult.trimToSize();

return searchResult;

}

}

publicclass MusicCd

{

private String musicCd;

privateint yearOfRelease;

public MusicCd()

{

musicCd ="";

yearOfRelease = 1900;

}

public MusicCd(String newMusicCd,int newYearOfRelease)

{

musicCd = newMusicCd;

yearOfRelease = newYearOfRelease;

}

public String getTitle()

{

return musicCd;

}

publicint getYearOfRelease()

{

return yearOfRelease;

}

publicvoid setTitle(String newMusicCd)

{

musicCd = newMusicCd;

}

publicvoid setYearOfRelease(int newYearOfRelease)

{

yearOfRelease = newYearOfRelease;

}

public String toString()

{

return("Music Cd`s Title: " + musicCd +"\t"

+"Year of release: " + yearOfRelease +"\t");

}

}

[8580 byte] By [Ivan1238a] at [2007-11-27 5:34:22]
# 1

mcs = new MusicCdStore(muiseCdsTitle, muiseCdsYearOfRelease);

public class MusicCdStore

{

private ArrayList<MusicCd> MusicCdList;

public MusicCdStore(String newMusicCdsTitle, int newYearOfRelease)

{

MusicCdList = new ArrayList<MusicCd>( ); // You create a new instance of Array List every time you call this method

MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));

MusicCdList.trimToSize();

}

If you want to add something without lose the previous one, you should not create a new instance.

You should put this statement somewhere else and call once only.

rym82a at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...
# 2
each time you enter a cd it looks like you're reconstructing your music CD store. Construct it only once. Have a public void addCD(....) method in musicCD store and use that to add CD's
petes1234a at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...
# 3
thx for u guy `s help....but could tell me much more specfic how to solve it...i get the idea of what u guy are suggesting me to do....thc at first , but i still not get it..pls...
Ivan1238a at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...
# 4
sorry .. i don`t know what i should acutally write to my code to solve this problm..pls...help
Ivan1238a at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...
# 5
Ivan, are you still stuck?
corlettka at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...
# 6
yes...corlettk...i try and try...pls give me some more direction....i have try this for one day already
Ivan1238a at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...
# 7

OK,

As stated by both previous posters, the guts of your trouble is that you're recreating your MusicCdStore (PS I think a better name would be CdCollection) every time you want to add a CD to the collection.... with this dodgy constructor codepublic MusicCdStore(String newMusicCdsTitle, int newYearOfRelease)

{

MusicCdList = new ArrayList<MusicCd>( );

MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));

//MusicCdList.add(new MusicCd(newMusicCdsTitle, newYearOfRelease));

//MusicCdList.add(new MusicCd("Get to the World"));

MusicCdList.trimToSize();

}

If I where you I'd turn the above constructor into a simple add method on the CdCollection class, like this.public void add(String title, int year) {

cds.add(new CD(title, year));

}

where cds is my List<CD> cds = new ArrayList<CD>; which is created ONCE in the no-arg constructor of my CdCollection class.

the manager looks somthing like thispackage forums;

import java.util.*;

public class CdManager {

public static void main(String[] args)

{

try {

CdCollection cds = new CdCollection();

String reponse = "";

Scanner in = new Scanner(System.in);

String title;

int year;

do {

System.out.println("Enter Your CD's Details :");

System.out.print("Title : "); title = in.nextLine();

System.out.print("Year : "); year = in.nextInt(); in.nextLine();

cds.add(title, year);

System.out.print("Enter another (y/n) ? "); reponse = in.nextLine();

System.out.println();

} while(reponse.equalsIgnoreCase("y"));

cds.print();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Note that I'm using a 1.6 standard Scanner in place of your custom keyboard-wrapper class.

corlettka at 2007-7-12 15:02:29 > top of Java-index,Java Essentials,New To Java...