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 2 of 2

Thread: Help with my Java applet

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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)

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with my Java applet

    I'm glad you posted the error, because a java.lang.StackOverflowError is the opposite of an "empty stack." The stack contains the breadcrumbs which the program will follow to retrace its steps, and your program has left so many breadcrumbs that the stack has become full. This usually occurs when a recursive method has no exit or when a method or constructor accidentally becomes recursive by calling itself. Check into that.

    Your two questions are probably related.

Similar Threads

  1. First Java Applet
    By makkushimu in forum Java Applets
    Replies: 2
    Last Post: June 1st, 2014, 03:00 PM
  2. java applet
    By laine in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 8th, 2013, 04:34 AM
  3. Need Help with this java applet
    By bruizer in forum Threads
    Replies: 5
    Last Post: September 23rd, 2012, 11:01 PM
  4. Running a java applet inside a java window?
    By frozen java in forum Java Theory & Questions
    Replies: 3
    Last Post: July 3rd, 2012, 10:55 PM
  5. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM