The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

please need help ... for the delete method in array

hi there, i really need help in doing a method which deletes a person from an array list. it is basically a dvd rental system that keeps track of clients, salesperson, dvds and loans. Currently I have done a Client class and a SalesPerson class that both inherit from the Person Class. Then I also have the DVD class, the DvdShop class, the Loan class and finally the runner class. Now, my problem is that I need the user to input the id of the person they want to delete from the array and the person is deleted. I have already done some of the code for the delete method my self but my problem is that i don't know where to put that line of code that says remove. ....
Here is the code so far;

//create an instance of DVD Shop, instances of Client and instances of Salesperson
DvdShop myDvdShop = new DvdShop();

int choice =0;

do
{
System.out.println("\n1 - To Add a new Client");
System.out.println("2 - To Add a Sales Person");
System.out.println("3 - to Add a DVD");
System.out.println("4 - To Show all Persons");
System.out.println("5 - To Delete a Client");
System.out.println("6 - To Show all DVDs");
System.out.println("7 - To Make a new DVD Loan");
System.out.println("8 - To Show all Loans");
System.out.println("9 - To Search for a Client or a SalesPerson");
System.out.println("10 - To save to file");
System.out.println("11 - To load from file");
System.out.println("12 - To exit\n");

System.out.println("Please enter ID of DVD being added");
newDVD.SetID(sc.nextInt());
System.out.println("Please enter name of DVD");
newDVD.SetName(sc.next());
System.out.println("Please enter year published");
newDVD.SetYear(sc.nextInt());
System.out.println("Please enter the amount of this DVD available in stock");
newDVD.SetInStock(sc.nextInt());

//Add to DvdShop
myDvdShop.AddDvd(newDVD);

break;

case 4:

//Show all Clients & SalesPersons

System.out.println(myDvdShop.showAllPersons());

break;

case 5:

//Delete a Client from the DvdShop
System.out.println("Enter ID of person you want to delete: ");
int indexPo = myDvdShop.removePerson(sc.nextInt());

if(indexPo!= -1)
{
//Person exists and his position in array list is retrieved
System.out.println(myDvdShop.removePerson(indexPo) );

Re: please need help ... for the delete method in array

When you get a compiler error saying that it cannot find the symbol, that means the variable you're trying to use is not available in the scope. Variables declared inside if statements or for loops are only available inside that statement. Variables declared inside methods are only available inside that method. Variables declared inside classes are only available inside that class. To access an instance from one class in another class, you have to pass a reference to that instance between the classes.

Re: please need help ... for the delete method in array

Originally Posted by yanikapausini:)

i don t actually know how to do that.. can you please give me an example similar to the one i need to do?? thanks very much for your replies

Which part don't you know how to do? If you want help, you'll have to provide an SSCCE that demonstrates the problem. In your case, two simple classes- one containing an ArrayList, and one that needs to delete something form that ArrayList. You've posted a lot of extra code. Don't forget the highlight tags. Chances are, during the process of narrowing your code down to an SSCCE, you'll figure it out all by yourself.

Re: please need help ... for the delete method in array

One more thing you might want to pay attention to while writing code is making classes separate. That way, when you find an error, you handle classes one at a time. Also, as said to you by someone in this forum, highlight your code with these tags [highlight = java] code here[/java] to make it easier for us to read and maybe help you with it.