public void deleteDvd(String title)
	{
		String DvdCompare;
		Dvd dvdObj;
		 Collections.sort(dvds);
		for(int i = 0;i< dvds.size();i++)
		{
			DvdCompare = dvds.get(i).getdvdTitle();
			dvdObj = dvds.get(i);
			if(title.equals(DvdCompare))
			{
			System.out.println("Dvd is in system");
			int index =Collections.binarySearch(dvds,DvdCompare);

			dvds.remove(index);
			//delete method in arrayList
			}
			else if (!title.equals(DvdCompare))
			{
				System.out.println("Not found Dvd Object");
			}

		}
		


	}

I'm tryin to search through my dvds arraylist, find title of dvd and delete it from the arraylist completely.

I have imported
import java.util.Collections;
import java.util.ArrayList;

E:\Java\dvd>javac Inventory.java
Inventory.java:172: cannot find symbol
symbol : method sort(java.util.ArrayList<Dvd>)
location: class java.util.Collections
Collections.sort(dvds);
^
Inventory.java:180: cannot find symbol
symbol : method binarySearch(java.util.ArrayList<Dvd>,java.lang.String)
location: class java.util.Collections
int index = Collections.binarySearch(dvds,DvdC
ompare);

I get this error above and can't seem to figure it out. any ideas please?
^

For first: why do You call Collections.binarySearch(...), when You already iterate through Your dvd store? If You encounter dvd, that's title equals to searched title, You already have its index ( -> i ), so binarySearch is not needed here.

For second: You'll get "Not found Dvd Object" message every iteration, when current dvd doesn't equals searched dvd.


Regarding to errors:

- "symbol : method sort(java.util.ArrayList<Dvd>)"

I think the problem is, that Your Dvd class doesn't implements Comparable interface, so that method sort, doesn't know which dvd is 'greater' and which is 'lesser'.

- Ad. binarySearch, don't know exactly how does searching by key look like, but propably again - method doesn't know what is the String key You give as argument. ( what if Dvd class has got 2 fields of String type? Which one is the given key? )

commented: helpfull +2

Thanks for the help i figured it out, just used dvds.remove(dvdObj); after all and didn't go ahead with the binarySearch method! how would i solve getting not found dvd object every iteration?

You need to check if dvd has been found AFTER You check Your whole dvd store, not during. So it can be for example something like that:

public void deleteDvd(String title)
{
	String DvdCompare;
	Dvd dvdObj;
	Collections.sort(dvds);
	
	boolean found = false;
	for( int i=0 ;i < dvds.size(); i++)
	{
		DvdCompare = dvds.get(i).getdvdTitle();
		dvdObj = dvds.get(i);
		if(title.equals(DvdCompare))
		{
			System.out.println("Dvd is in system");
			dvds.remove(i);
			
			found = true;
					
			break;
		}
	}
	
	if( !found )
		System.out.println("Dvd not found in system");
}

Oh rite, ya i get it now, thanks for all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.