Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

syntax error defining a method

Status
Not open for further replies.

gemann

Technical User
May 21, 2002
9
CA
Hi, I'm trying to pull a card from a stack in TryDeal class. I've defined a method getCard() in Hand class. I'm getting a syntax error when I compile:

C:\java\War>javac TryDeal.java
.\Hand.java:19: incompatible types
found : java.lang.Object
required: Card
return hand.pop();
^
1 error
Any help would be greatly appreciated....

import java.util.*;

class Hand
{

public void add(Card card)
{
hand.push(card);
}

public Card getCard()
{
return hand.pop();
}


public String toString()
{
Iterator cards = hand.iterator();

StringBuffer str = new StringBuffer();
while(cards.hasNext())
str.append(" "+ (Card)cards.next());
return str.toString();
}



private Stack hand = new Stack(); // Stores the card
}
************************************************************

import java.util.*;
class TryDeal
{
public static void main(String[] args)
{
CardDeck deck = new CardDeck();
deck.shuffle();

Hand myHand = deck.dealHand(5);
Hand yourHand = deck.dealHand(5);

Card myCard = (Card)myHand.getCard();

System.out.println("\nMy hand is"+myCard);
}
}
Thanks.......gemann
 
Collections all keep objects, so you always have to cast them back to their original type or an interface that they all implement.

There are two things I suggest you do to your code: import only the class(es) you need, and locate all your class variables at the top of the code, right after the class declaration. The first one is neater, the other one standard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top