Results 1 to 4 of 4
  1. #1
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Help with my Java applet

    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)

    Java Code:
    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;
    	}
    }
    Java Code:
    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();
    	}
    
    }
    Java Code:
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with my Java applet

    You need to post the full text of the error message. Your comment mentions a empty stack error (did you mean: EmptyStackException and the error message that was posted says: StackOverflowError

    If there was a StackOverflowError please only post enough of the error messages to show where the error message text repeats.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with my Java applet

    Yes I mean EmptyStackException when using the method buildDeck (line 12 of my Table class) and then the method removeCard (line 13 of my Table class) that's when I catch the empty stack error and I print to the console (Error, empty stack : ( on line 19 of my Deck class).

    I am using a method to build my deck of cards but I think it would be best to use a constructor. I commented out the constructor on line 32 of my Deck class and replaced it with the buildDeck method because I am getting the following error when using the constructor:

    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) < repeating
    Last edited by DFORMS; 09-14-2014 at 10:14 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with my Java applet

    You have a good start on adding println() statements for debugging.
    But more are needed. Print out the value of deck when it is created and when it is used to see if you are using the same object. It should print something like this: Deck@143316c
    The hex number at the end should be unique for each object created. If the values are different then the code must create more than one object, puts data into one and tries to get data from the other which is empty.
    Last edited by Norm; 09-14-2014 at 10:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 06-08-2012, 07:11 AM
  2. Replies: 0
    Last Post: 04-26-2012, 11:50 AM
  3. Replies: 30
    Last Post: 08-26-2011, 09:05 PM
  4. Java Applet to Applet communication
    By jsman in forum Java Applets
    Replies: 3
    Last Post: 04-05-2011, 02:23 PM
  5. applet class inside java.applet.* or java.awt.*
    By Manish87 in forum Java Applets
    Replies: 1
    Last Post: 09-27-2010, 02:15 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
  •