-1

I'm nearing the end of my program and I've hit a snag that I don't know how to fix.

I'm trying to compare the rank value of two cards, so I use a method to get the rank of the card that was just removed from the queue. However, I can't convert the card to int values. I'm a little confused by this because the Card is simply two int values, rank and suit which are then converted to strings via the cRank/cSuit arrays.

This is the specific code that is causing the error

x = Card.getRank(PlayerOne.remove()); //Incompatible types: Card cannot be converted to int

Here is the code, and if you see anything that makes you want to cry just point it out.

Card class

package randomdeck;
import java.util.ArrayList;
import java.util.Stack;

public class Card 
{
int suit;
int rank;

public String[] cRank = new String[] { "Ace", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "Jack",
"Queen", "King" };

public String[] cSuit = new String[] { "Hearts",
"Spades", "Diamonds", "Clubs" };


public Card(int rank, int suit)
{
    this.suit=suit;
    this.rank=rank;
}

int getRank(int rank)
{
    return rank;
}

int getSuit(int suit)
{
    return suit;
}

void setSuit()
{
    suit=Card.this.suit;
}

void setRank()
{
    rank=Card.this.rank;
}

 public String toString()
{
    return cRank[rank] + " of " + cSuit[suit];
}
}

Deck Class

public class Deck 
{
Stack<Card> Deck = new Stack<Card>();

 public Deck()
 {

   for(int rank=0; rank<13; rank++)
   {
       for(int suit=0; suit<4; suit++)
       {
           Deck.push(new Card(rank,suit));

       }
   }
}

public void peekDeck()
{
   System.out.println(Deck); 
}

public void printDeckSize()
{
   System.out.println("DECK " + Deck.size());
}

public int getDeckSize()
{
   Deck.size();
   return Deck.size();

}

public Stack popDeck()
{
   Deck.pop();
   return Deck;
}

public Stack getDeck()
{
   return Deck;
}

public void shuffleDeck()
{
   long seed = System.nanoTime();
   //Collections.shuffle(Deck, new Random(seed));  //MichealScot.no/gif
   ArrayList<Card> temp = new ArrayList<Card>();

   while(Deck.size()!=0)
   {
       temp.add(Deck.pop());
   }

   Collections.shuffle(temp, new Random(seed));
   System.out.println(temp);

   Deck.addAll(temp);
   temp.clear();
   System.out.println(temp);


}

Wargame Class (CLASS WITH ERROR)

public class Wargame extends Deck
{
Queue<Card> PlayerOne = new LinkedList<Card>(); 
Queue<Card> PlayerTwo = new LinkedList<Card>(); //Stacks that will be used to hold players winnings/starting deck

public Wargame()
{

}

void splitDeck()
{

    while(PlayerOne.size()<getDeckSize())
    {   
        PlayerOne.add(Deck.pop());
    }
    //System.out.println(PlayerOne);

    int x = Deck.size();

    for(int i=0; i<x; i++)
    {
        PlayerTwo.add(Deck.pop());
    }
    //System.out.println(PlayerTwo);

}


public void Game()
{

    shuffleDeck();
    splitDeck();
    ArrayList Temp = new ArrayList();

    while(PlayerOne.size()!=0 || PlayerTwo.size()!=0)
    {
        int x=0;
        int y=0;
        //Temp.add(PlayerOne.remove());
        //Temp.get(0);

        x = Card.getRank(PlayerOne.remove());
        y = Card.getRank(PlayerTwo.remove());   //Incompatable types, Card cannont be     converted to int

    }
}
user50017
  • 3
  • 2
  • 5

2 Answers2

1

PlayerOne.remove() returns a Card object. The Card.getRank() method takes an int.

To make matters worse, Card.getRank() is an instance method (not static) but you're calling it as if it were static (as Card.getRank() instead of someCardObject.getRank()).

Make sure you're totally solid on your understanding of static vs. instance methods. Then look over your code again, keeping in mind what types your methods take as parameters and what types your methods return. You should then be able to go through and fix your code.

Community
  • 1
  • 1
TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • Ok, I changed the code so I have Card objCard = new Card(); x = objCard.getRank(PlayerOne.peek()); However this still doesn't solve my issue of attempting to get the rank of the card that is peeked from the top of the deck. I'll try messing around with some code, but I'm not sure where to start. I understand that I was returning an int when calling a Card, my thought process was because the Card constructor is made of two ints, the rank and suit, I could call one with a method and return it into a variable to be used later. Obviously that doesn't work – user50017 Mar 15 '14 at 21:08
0

The error message is extremely clear here:

PlayerOne.remove()

This method returns an object of type Card, however you're attempting to pass this Object into the getRank method. The signature for the getRank method specifies that it takes one argument of type int. The compiler attempts to check if they are compatible, which they are not, hence the compile time error, and the error message:

Incompatible types: Card cannot be converted to int

It literally means what it says. Card and int are not interchangable, so one can not be used in place of the other when making method calls.

christopher
  • 26,815
  • 5
  • 55
  • 89