Results 1 to 10 of 10
  1. #1
    Collider is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Method to return object

    Hey, I am working on this CW where I have an Array list which has all the object saved in. And there is an unique ID assigned to every object. This isnt problem this works fine. My problem now is I need to make a method which will return object depending on the unique ID.

    My question is how do i declare the method ? whatever I try it just says error "invalid method type"...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Method to return object

    SHow us what you've tried, and the full text of any compilation errors.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Collider is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: Method to return object

    well this is my method to add new player


    void regPlayer(Player p){ //passing the player object
    playerArray.add(p); // adding it to the arraylist
    p.setReg(RegNum()); // gives the player object an unique ID
    sort(); // this is just to sort the arraylist in alphabetical order
    }

    now i need another method which will return player object, depending on which player ID was passed into the method.

    so something like

    requestPlayer(int i){
    return playerArray.get(i);

    }

    I am not sure how to actually do this. And when i hover over the requestPlayer its saying "invalid method declaration; return type required"

    And the method requires a type. Or can I just leave it as void ?
    Last edited by Collider; 05-01-2012 at 06:50 PM.

  4. #4
    Collider is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: Method to return object

    oh im dumb... im returning object Player so type has to be Player requestPlayer(int i) >.<

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Method to return object

    There you go.
    However, this will return the Player at index 'i' in the array:
    Java Code:
    return playerArray.get(i);
    In order to get the Player with the regNum == i you'll have to loop through the List and compare them, returning the one that matches.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Collider is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: Method to return object

    Yess thanks a lot =) that's what I have done. (do i mark this as solved somehow ?)

  7. #7
    Collider is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: Method to return object

    haha people are stalking me here lol and its nearly done :P handing it in today

  8. #8
    NewcNeil is offline Banned
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Method to return object

    what is this?!?!

  9. #9
    NewcNeil is offline Banned
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Method to return object

    you boys are in serious trouble!!

  10. #10
    Collider is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

Similar Threads

  1. About the method which return an object
    By nihirus in forum New To Java
    Replies: 6
    Last Post: 02-22-2012, 12:10 AM
  2. Return custom object in Java Webservice
    By Nela in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-23-2012, 04:58 PM
  3. Return an object to use in another method
    By TidusSolan in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 08:00 PM
  4. How I can return an object?
    By CFW in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 10:54 AM
  5. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 06:57 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
  •