Hi guys,
I have two questions.
1 - I don't understand why I'm getting an empty stack error when calling the removecard method in my Table class line 13?
2 - I am trying to use a constructor to populate my deck of cards instead of a method when creating my theDeck object in my Table class line 11 but I get the following error:
java.lang.StackOverflowError
at java.util.Vector.<init>(Unknown Source)
at java.util.Vector.<init>(Unknown Source)
at java.util.Stack.<init>(Unknown Source)
at Deck.<init>(Deck.java:7)
at Deck.<init>(Deck.java:34)
public class Card { String enseigne; int valeur; public String toString(){ String v, l, x; switch(valeur){ case 1: v = "As"; break; case 2: v = "deux"; break; case 3: v = "trois"; break; case 4: v = "quatre"; break; case 5: v = "cinq"; break; case 6: v = "six"; break; case 7: v = "sept"; break; case 8: v = "huit"; break; case 9: v = "neuf"; break; case 10: v = "dix"; break; case 11: v = "Valet"; break; case 12: v = "Dame"; break; case 13: v = "Roi"; break; default: v = "oh no, SKYNET!"; } if(v == "Dame"){ l = "La "; } else if(v == "As"){ l = "L'"; } else { l = "Le "; } if(v == "oh no, SKYNET!"){ x = "Cheater!!"; } else{ x = l + v + " de " + enseigne; } return x; } }
import java.util.Collections; import java.util.EmptyStackException; import java.util.Stack; public class Deck { Stack<Card> stackedCards = new Stack<Card>(); public void addCard(Card x){ stackedCards.push(x); System.out.println("Cards in deck: " + stackedCards.size()); } public Card removeCard(){ Card card0 = new Card(); try{ card0 = stackedCards.pop(); }catch(EmptyStackException e){ System.out.println("Error, empty stack : ("); } return card0; } public void shuffleCards(){ try{ Collections.shuffle(stackedCards); }catch (EmptyStackException e){ System.out.println("Error, empty stack : ("); } } /* Deck(){ Constructor not working : s */ public void buildDeck(){ Deck deck = new Deck(); for(int i=0; i<52; i++){ Card card0 = new Card(); if(i<13){ card0.enseigne = "Piques"; card0.valeur = i+1; } else if(i<26){ card0.enseigne = "Carreaux"; card0.valeur = i-12; } else if(i<39){ card0.enseigne = "Coeurs"; card0.valeur = i-25; } else{ card0.enseigne = "Trefles"; card0.valeur = i-38; } System.out.println(card0.toString()); deck.addCard(card0); } deck.shuffleCards(); } }
import javax.swing.*; public class Table extends JApplet { private static final long serialVersionUID = 2501; public void init(){ System.out.println("init()..."); Deck theDeck = new Deck(); theDeck.buildDeck(); theDeck.removeCard(); } public void start(){ System.out.println("start()..."); } public void stop(){ System.out.println("stop()..."); } public void destroy(){ System.out.println("destroy()..."); } }
Thank you for your time.