Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to get the return of a method inside an object in an ArrayList?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default How to get the return of a method inside an object in an ArrayList?

    So here is my predicament.
    I have an ArrayList called dealersHand.
    I also have an ArrayList Deck that is full of playing Card objects. the Card class contains a toString method that returns only the suit and the face of the card. But inside the Card class there is also a getValue() method with a return type of int. That holds the cards value.
    What I've been doing in my program is .add(#) ing objects from the Deck arraylist directly into the dealersHand arraylist which initially, when the program runs, has nothing inside of it at all.
    So my question here is. Once I put a Card object inside dealersHand array list. how can I access an individual cards getValue method?

    dealersHand.get(#).getValue(); sure isn't working =(.
    I've tried finding answers that actually make sense on the internet but no luck yet So thank you much for any help. =]

    If you guys need I'll collect some of the code so you can look at what I'm trying to do, But i think its pretty clearly explained above?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to get the return of a method inside an object in an ArrayList?

    What exception are you getting (please post the message you're gettting)? Your description looks like it should work, though I can see one potential problem when it comes to generic typing.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Hallowed (May 1st, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to get the return of a method inside an object in an ArrayList?

    Well I think my problem is this, the code wont compile at all. And I think its because I'm trying to compile it, but when I have this code in there: dealersHand.get(#).getValue(); it doesn't compile because .getValue(); isn't in the dealersHand arraylist yet. Nothing is in it at all until the program starts running. I've changed my code to work around this, but I'll reconstruct it to see what kind of an error I'm getting specifically, and post my code here.

  5. #4
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to get the return of a method inside an object in an ArrayList?

    This was the error I was getting...
    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method getValue() is undefined for the type Object"
    Last edited by Hallowed; May 1st, 2011 at 02:47 PM.

  6. #5
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to get the return of a method inside an object in an ArrayList?

    Well never mind. o_O
    I casted it like this:

    ((Card) dealersHand.get(i)).getValue();

    And it worked fine. I'm pretty sure I tried that before and it wouldn't go! But now it does... So thanks for your help sir haha.

    And sorry for the triple post. But they didn't seem unreasonable until I edited them.
    Last edited by Hallowed; May 1st, 2011 at 02:49 PM.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to get the return of a method inside an object in an ArrayList?

    You're running into that problem because you're not specializing your ArrayList. Casting it to a Card will work, but it's not a good solution since it's possible that dealersHand could contain something other than Cards. Rather, declare dealersHand as type ArrayList<Card>.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    Hallowed (May 1st, 2011)

  9. #7
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to get the return of a method inside an object in an ArrayList?

    oh wow I can't believe I forgot to do that o_O. Thanks that will help me in future programming.

    If I did have something other than a Card object in there, and for that reason I wanted to use casting and I tried to do dealersHand.get(i)).getValue(); with something that didn't have a getValue() method in it, it would make my program crash right?
    So would my only solution would be, is to check somehow, if its a card first in an if statement or something. right?
    like:
    if (dealersHand.get(i).getType() == "Card" )
    {
        dealersHand.get(i)).getValue();
    }
    Or is there some code I can use to check objects specifically? Providing I know that there will only be objects in that array.
    Last edited by helloworld922; May 1st, 2011 at 10:43 PM.

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to get the return of a method inside an object in an ArrayList?

    use the instanceof operator:

    if(dealersHand.get(i) instanceof Card)
    {
        // guaranteed to be safe to cast to Card
    }

Similar Threads

  1. [SOLVED] Accessing methods of an object inside in ArrayList
    By jmack in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 12:28 PM
  2. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM
  3. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM
  4. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM