Results 1 to 9 of 9
  1. #1
    Gmurph03 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    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?

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    PRINT THE ERROR!!!

    by the way, does studentlist.item(0)=null; works?

  3. #3
    Gmurph03 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    Java 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.

  4. #4
    Gmurph03 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    AddressBook is just a renamed studentlist by the way!

  5. #5
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    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

  6. #6
    Gmurph03 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    OK let me try again with code.

    Java 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.

    Java 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 04:30 AM.

  7. #7
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    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?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    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:

    Java Code:
    def remove(int ndx){
        // do stuff to ensure ndx is a valid index and, if it is,
        // remove the item from the array.
    }

  9. #9
    Gmurph03 is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    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 12:49 PM.

Similar Threads

  1. Creating new instances of a Match over and over again
    By Che_Is_Alive in forum Advanced Java
    Replies: 2
    Last Post: 11-19-2009, 07:05 PM
  2. Problem with class instances
    By sdwinder in forum New To Java
    Replies: 7
    Last Post: 10-21-2009, 02:25 AM
  3. Build Instances problem
    By leapinlizard in forum New To Java
    Replies: 4
    Last Post: 04-21-2009, 11:17 AM
  4. Naming object instances
    By oldgit in forum New To Java
    Replies: 9
    Last Post: 02-08-2008, 12:18 AM
  5. Replies: 0
    Last Post: 12-04-2007, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •