ArrayList problem ....i can remove my object from my arrayList

hi all, i am going to remove a object from my array by using arrayList.However, it can`t work properly and did nth for me..i don`t know why...could anyone give me some suggestion and show me what i did wrong on my code ....i stated more detail next to my code...plesae help...

publicclass MusicCd

{

private String musicCdsTitle;

privateint yearOfRelease;

public MusicCd()

{

musicCdsTitle ="";

yearOfRelease = 1900;

}

public MusicCd(String newMusicCdsTitle)

{

musicCdsTitle = newMusicCdsTitle;

//yearOfRelease = newYearOfRelease;

}

public MusicCd(String newMusicCdsTitle,int newYearOfRelease)

{

musicCdsTitle = newMusicCdsTitle;

yearOfRelease = newYearOfRelease;

}

public String getTitle()

{

return musicCdsTitle;

}

publicint getYearOfRelease()

{

return yearOfRelease;

}

publicvoid setTitle(String newMusicCdsTitle)

{

musicCdsTitle = newMusicCdsTitle;

}

publicvoid setYearOfRelease(int newYearOfRelease)

{

yearOfRelease = newYearOfRelease;

}

/*

public boolean equalsName(MusicCd otherCd)

{

if(otherCd == null)

return false;

else

return (musicCdsTitle.equals(otherCd.musicCdsTitle));

}

*/

public String toString()

{

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

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

}

}

import java.util.ArrayList;

import java.io.*;

publicclass MusicCdStore

{

ArrayList<MusicCd> MusicCdList;

publicvoid insertCd()

{

MusicCdList =new ArrayList<MusicCd>( );

readOperation theRo =new readOperation();

MusicCd theCd;

int muiseCdsYearOfRelease;

String muiseCdsTitle;

while(true)

{

String continueInsertCd ="Y";

do

{

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

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

MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));

MusicCdList.trimToSize();

continueInsertCd = theRo.readString("Do you have another Cd ? (Y/N) : ");

}while(continueInsertCd.equals("Y") || continueInsertCd.equals("y") );

if(continueInsertCd.equals("N") || continueInsertCd.equals("n"));

{

//MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));

break;

}

//System.out.println("You `ve an invalid input " + continueInsertCd + " Please enter (Y/N) only!!");

}

}

publicvoid displayAllCd()

{

System.out.println("\nOur CD collection is: \n" );

System.out.println(toString());

}

public String toString( )

{

String result=" ";

for( MusicCd tempCd : MusicCdList)

{

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

}

return result;

}

publicvoid searchingMusicCd()

{

readOperation theRo =new readOperation();

String keyword = theRo.readString("Enter a CD `s Title you are going to search : ") ;

ArrayList<MusicCd> results = searchForTitle(keyword );

System.out.println("The search results for " + keyword +" are:" );

for(MusicCd tempCd : results)

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

}

//encapsulate the A

publicvoid removeCd()

{

readOperation theRo =new readOperation();

String keyword = theRo.readString("Please enter CD `s title you are going to remove : ") ;

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

System.out.println("The CD that you just removed is " + keyword );

for(MusicCd tempCd : removeMusicCdResult)

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

}

//problem occurs here : i am so confused of how to remove the exactly stuff from my arrayList

//pls help

private ArrayList<MusicCd> removeCdForTitle(String removeCdsTitle)

{

MusicCd tempMusicCd =new MusicCd();

tempMusicCd.setTitle(removeCdsTitle);

// tempMusicCd.setTitle(removeCdsTitle);

//tempMusicCd.getTitle() = removeCdsTitle;

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

for(MusicCd currentMusicCd : MusicCdList)

{

if((currentMusicCd.getTitle()).equals(tempMusicCd.getTitle()))

// removeMusicCdResult.remove(currentMusicCd);

MusicCdList.remove(currentMusicCd);

}

removeMusicCdResult.trimToSize();

return removeMusicCdResult;

}

private 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;

}

}

import java.util.*;

publicclass MusicCdStoreEngine{

publicstaticvoid main(String[] args)

{

MusicCdStore mcs =new MusicCdStore( );

mcs.insertCd();

//display the Cd that you just insert

mcs.displayAllCd();

mcs.removeCd();

mcs.displayAllCd();

mcs.searchingMusicCd();

}

}

//Acutally result

//Please enter your CD`s title : ivan

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

//Do you have another Cd ? (Y/N) : y

//Please enter your CD`s title : hero

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

//Do you have another Cd ? (Y/N) : n

//Our CD collection is:

// Music Cd`s Title: ivanYear of release: 1992

//Music Cd`s Title: heroYear of release: 1992

//Please enter CD `s title you are going to remove : hero

//The CD that you just removed is hero

//

//Our CD collection is:

// Music Cd`s Title: ivanYear of release: 1992

//Music Cd`s Title: heroYear of release: 1992

//

//Enter a CD `s Title you are going to search : hero

//The search results for hero are:

//Music Cd`s Title: heroYear of release: 1992

//>Exit code: 0

/////////////////////////////////////////////////////

//Expected result

//Please enter your CD`s title : ivan

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

//Do you have another Cd ? (Y/N) : y

//Please enter your CD`s title : hero

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

//Do you have another Cd ? (Y/N) : n

//Our CD collection is:

// Music Cd`s Title: ivanYear of release: 1992

//Music Cd`s Title: heroYear of release: 1992

//Please enter CD `s title you are going to remove : hero

//The CD that you just removed is hero

//

//Our CD collection is:

// Music Cd`s Title: ivanYear of release: 1992

//Music Cd`s Title: heroYear of release: 1992<<-- it is not supposed to display cos i have deleted it from from array

//

//Enter a CD `s Title you are going to search : hero

//The search results for hero are:

//Music Cd`s Title: heroYear of release: 1992<<-- i should have get this reuslt...cos it is already delete from my array

//>Exit code: 0

import java.util.*;

publicclass readOperation{

public String readString(String userInstruction)

{

String aString =null;

try

{

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

aString = scan.nextLine();

}

catch (NoSuchElementException e)

{

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

}

catch (IllegalStateException e)

{

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aString;

}

publicchar readTheFirstChar(String userInstruction)

{

char aChar =' ';

String strSelection =null;

try

{

//char charSelection;

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

strSelection = scan.next();

aChar = strSelection.charAt(0);

}

catch (NoSuchElementException e)

{

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

}

catch (IllegalStateException e)

{

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aChar;

}

publicint readInt(String userInstruction){

int aInt = 0;

try{

Scanner scan =new Scanner(System.in);

System.out.print(userInstruction);

aInt = scan.nextInt();

}catch (InputMismatchException e){

System.out.println("\nInputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) " + e);

}catch (NoSuchElementException e){

System.out.println("\nNoSuchElementException error occurred (input is exhausted)" + e);

}catch (IllegalStateException e){

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aInt;

}

}

[17376 byte] By [Ivan1238a] at [2007-11-27 5:36:18]
# 1

> //problem occurs here

I'm not sure that the problem does occur within the

removeCdForTitle() method.

Your main() method calls removeCd() which obtains the title of

the CD to be removed (keyword). But remoceCd() never

calls removeCdForTitle(), so nothing is ever removed.

pbrockway2a at 2007-7-12 15:06:47 > top of Java-index,Java Essentials,New To Java...
# 2
Code changed a bit and multiposted. Reply here: http://forum.java.sun.com/thread.jspa?threadID=5176986
pbrockway2a at 2007-7-12 15:06:47 > top of Java-index,Java Essentials,New To Java...