ArrayList problem ....how can i remove my object.

i am going to insert a cd to the arrayList , remove a cd from the arrayList, searchTheTitle, and display it...But i stuck on my remove function...i can `t remove the data that i want from the array..pls help..

//MusicCd enscaslaute the cd data

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 =searchForRemoveCdsTitle(keyword);

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

for(MusicCd tempCd : removeMusicCdResult)

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

}

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;

}

private ArrayList<MusicCd> searchForRemoveCdsTitle(String searchString)

{

MusicCd tempCd =new MusicCd();

tempCd.setTitle(searchString);

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

for(MusicCd currentMusicCd : MusicCdList)

{

if((currentMusicCd.getTitle()).equals(tempCd.getTitle()))

removeCdsResult.remove(currentMusicCd);

}

removeCdsResult.trimToSize();

return removeCdsResult;

}

}

/*

private ArrayList<MusicCd> searchForRemoveCdsTitle(String searchString)

{

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

for(MusicCd currentMusicCd : MusicCdList)

{

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

removeCdsResult.remove(currentMusicCd);

}

removeCdsResult.trimToSize();

return removeCdsResult;

}

*/

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;

}

}

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

[17420 byte] By [Ivan1238a] at [2007-11-27 5:36:26]
# 1

Cripes that's a lot of code. I didn't read through all of it, but here's about what you do:

I'm assuming you won't be doing any best-approximation searching.

Create a method that matches a name and returns the index of the name, then use the ArrayList.remove() on the index.

Or, heck, you should be able to just use ArrayList.remove(name).

Joe

Joe_ha at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...
# 2
first ..thc for your reply...i don`t get what the suggestion u give me...could u pls show me more clear ? i am just a new comer ..thxc
Ivan1238a at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...
# 3

In removeCd() you call searchForRemoveCdsTitle(keyword) to build up a list of CDs to remove. But there are two things wrong:

(1) searchForRemoveCdsTitle() removes rather than adds the CDs that it finds to the list that it returns.

(2) In removeCd() you never actually remove the CDs from the array list, you just print them to System.out

pbrockway2a at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...
# 4
thc for your suggestion.. i think u understand what i am trying to do with my programming...but ..i don`t quite get it what i need to solve it....could u show me more specfic..pls ....i need help ..<_>
Ivan1238a at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...
# 5

> (1) searchForRemoveCdsTitle() removes rather than adds the CDs that it finds to

> the list that it returns.

So searchForRemoveCdsTitle() should be:private ArrayList<MusicCd> searchForRemoveCdsTitle(String searchString)

{

MusicCd tempCd = new MusicCd();

tempCd.setTitle(searchString);

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

for(MusicCd currentMusicCd : MusicCdList)

{

if((currentMusicCd.getTitle()).equals(tempCd.getTitle()))

//removeCdsResult.remove(currentMusicCd);

removeCdsResult.add(currentMusicCd); // <-- changed

}

removeCdsResult.trimToSize();

return removeCdsResult;

}

> (2) In removeCd() you never actually remove the CDs from the array list, you just

> print them to System.out

So removeCd() should be:public void removeCd()

{

readOperation theRo = new readOperation();

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

ArrayList<MusicCd> removeMusicCdResult =searchForRemoveCdsTitle(keyword);

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

for(MusicCd tempCd : removeMusicCdResult)

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

MusicCdList.remove(tempCd); // <-- changed

}

With these changes we get:Please enter your CD`s title : ivan

Please enter your CD`s year of release : 1234

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

Please enter your CD`s title : hero

Please enter your CD`s year of release : 4321

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

Our CD collection is:

Music Cd`s Title: ivanYear of release: 1234

Music Cd`s Title: heroYear of release: 4321

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: 1234This "works", but I can't do more than hope that it helps. Really if something is unclear about either of these points it might be more productive to ask about that.

pbrockway2a at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...
# 6
thx for all of u ..esppecially for "pbrockway2 "..i `ve spent lot of time on this problem ..and i can`t find where it occurs....so i am happy for that.. i can sleep now.. hhaha...thx for all...
Ivan1238a at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...
# 7

I know your problem has been solved, but I hope you'll see this and remember it for next time.

That's *WAY* too much code.

If your problem is removing something from a List, you should write a very small program that just adds to and removes from a List--just hardcoded values, no user input, no GUI, etc. Just the List. Chances are, you'll be able to get that working on your own and then incorporate it into your larger program. If not, you can post that small program and people can see easily what you're doing wrong.

jverda at 2007-7-12 15:07:07 > top of Java-index,Java Essentials,New To Java...