Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2009, 07:38 PM
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
Gmurph03 is on a distinguished road
Default Deleting instances of classes from a list
So I have a class 'studentlist' and another class 'student' which goes into a list in studentlist.

I then have a script that creates a new instance of studentlist and creates instances of 'student' that go into studentlist's' list.

However when I try to manipulate the list such as writing

studentlist.remove(0)

to remove the first instance of the student class in studentlist, the compiler returns an error. what is going wrong exactly? i have obviously used this type of code many times before to remove certain integers from lists, according to their index number, but when you have a list of class instances it doesn't work. Can anyone tell me why?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-26-2009, 09:08 PM
Senior Member
 
Join Date: Nov 2009
Posts: 139
Rep Power: 0
dinosoep is on a distinguished road
Default
PRINT THE ERROR!!!

by the way, does studentlist.item(0)=null; works?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-26-2009, 09:14 PM
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
Gmurph03 is on a distinguished road
Default
Code:
Caught: groovy.lang.MissingMethodException: No signature of method: AddressBook.remove() is applicable for argument types: (java.lang.Integer) values: [0]
	at CWTWOBSCRIPT.run(CWTWOBSCRIPT.groovy:36)
It's in groovy, but obviously any java solution is a groovy solution.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2009, 09:15 PM
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
Gmurph03 is on a distinguished road
Default
AddressBook is just a renamed studentlist by the way!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2009, 09:23 PM
Senior Member
 
Join Date: Nov 2009
Posts: 139
Rep Power: 0
dinosoep is on a distinguished road
Default
as this forum doesnt let me post links this is the incomplete one:
old.nabble.com/Closure-vs-function-td15496180.html

by the way. I don't know if this is the solution but its groovy, don't even know what it is but those guys seems to have the same problem+solution
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-27-2009, 03:26 AM
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
Gmurph03 is on a distinguished road
Default
OK let me try again with code.

Code:
class AddressBookEntry {
      String name
      String address
      String telNum
      String eMail


      AddressBookEntry(name, address, telNum, eMail){
        this.name = name
        this.address = address
        this.telNum = telNum
        this.eMail = eMail

      }
  String toString(){
    return """
    Name: $name ; Address: $address ; Tel No: $telNum ; E-Mail: $eMail """
    }


}

//

class AddressBook {
	private entrylist

	AddressBook(){
		entrylist = []
	}

	def add(AddressBookEntry abe){
		entrylist << abe
	}

   
	String toString(){
		String s = ""
		for (item in entrylist){
			s = s + item
		}
		return s
       }
}


//

int loopControl = 0
def addbook = new AddressBook()

while (loopControl < 1) {
print """
Please make a choice from the following:

* Add entry (press 1)
* Delete entry (press 2)
* View all entries (press 3)
* Update an entry (press 4)
* Exit program (press 5)
"""
int navigate = new Scanner(System.in).nextInt()

if (navigate == 1) {
println "What is the Name?"
String newName = new Scanner(System.in).nextLine()
println "What is the Address?"
String newAddress = new Scanner(System.in).nextLine()
println "What is the Telephone Number?"
String newTelNum = new Scanner(System.in).nextLine()
println "What is the E-Mail address?"
String newEMail = new Scanner(System.in).nextLine()

println """
New Address Book Entry Confirmed!"""

def newAdd = new AddressBookEntry("$newName", "$newAddress", "$newTelNum", "$newEMail")
addbook.add(newAdd)
}


if (navigate == 2){
addbook.remove(0)
}
The first class is the AddressBookEntry class, which feeds into the list in the second AddressBook class. The last part is the script which creates a new AddressBook and then creates new AddressBookEntry classes to go into that class. The add function works fine, but the remove one at the end there throws up the following error.

Code:
Caught: groovy.lang.MissingMethodException: No signature of method: AddressBook.remove() is applicable for argument types: (java.lang.Integer) values: [0]
	at ISDTWOBSCRIPT.run(ISDTWOBSCRIPT.groovy:36)
For the code written I would expect it to delete the first instance of the AddressBookEntry class that was instantiated in AddressBook's list as I am referring to that by it's index number, but it's not working. Is this due to the list in AddressBook being full of instantiated classes and not stuff like ints or strings?

Last edited by Gmurph03; 11-27-2009 at 03:30 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-27-2009, 07:24 AM
Senior Member
 
Join Date: Nov 2009
Posts: 139
Rep Power: 0
dinosoep is on a distinguished road
Default
I really have no clue.
but in your case, write a new delete function.
its not much work and then it just has to work.
Sorry I can't help you but writing a delete function is easily done?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-27-2009, 08:25 AM
Senior Member
 
Join Date: Feb 2009
Posts: 661
Rep Power: 2
pbrockway2 is on a distinguished road
Default
I haven't used this language, but since this is a new to Java forum I'll give you the benefit of my ignorance...

Doesn't the error message say it all? Your AddressBook class has an add() method so you can call add() with the new entry. But you get an error when you call remove() because there is no remove() method defined anywhere.

Try adding the following to the definition of AddressBook:

Code:
def remove(int ndx){
    // do stuff to ensure ndx is a valid index and, if it is,
    // remove the item from the array.
}
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-27-2009, 11:41 AM
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
Gmurph03 is on a distinguished road
Default
Hey guys I had written a delete funntion in AddressBook previously but that also wasn't working, I guess I should probably go back to that and try making it work.

Also I am programming in Groovy, which is a superset of Java, so everything is more or less identical, in Groovy the remove function is a library one and doesn't need a method written for it.

So I have no idea what's going on.

Thanks for the help anyway, much appreciated.

Last edited by Gmurph03; 11-27-2009 at 11:49 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating new instances of a Match over and over again Che_Is_Alive Advanced Java 2 11-19-2009 06:05 PM
Problem with class instances sdwinder New To Java 7 10-21-2009 01:25 AM
Build Instances problem leapinlizard New To Java 4 04-21-2009 10:17 AM
Naming object instances oldgit New To Java 9 02-07-2008 11:18 PM
Short key for getting list of opened classes in Eclipse Java Tip Java Tips 0 12-04-2007 11:11 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:17 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org