Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Cannt find symbol - how do I fix this/why isn't it working?

    I'm trying to compile this class and it's giving me the error that it can't find symbol for the variable "game".
    I"m really new to this and was/am learning from the text - it was working from an example from the book, but now I put in my examples and it's not working.

    What am I doing wrong??? Starting to give up.

    Java Code:
    public class Card {
        private String suit;
        private int rank;
    
        public Card(){
            suit = "Spades";
    		rank = 0;
        }
        /**
         * @return suit
         */
        public String getSuit() {
            return suit;
        }
    
        /**
         * @param suit the suit to set
         */
        public void setSuit(String suit) {
            this.suit = suit;
        }
    
        /**
         * @return the rank
         */
        public int getRank() {
            return rank;
        }
    
        /**
         * @param rank the rank to set
         */
        public void setRank(int rank) {
            this.rank = rank;
        }
        
        public static void main(String[] args){
            //Make it a game;
            Card game = new Card();
    		game.setSuit("Spades");
    		game.setRank(0);
    	}
    	public void play(){
    	System.out.println("Your first card is a " + game.getSuit() + "of " + game.getRank());
    	/*System.out.println("Your second card is a " + game.getSuit + " of " + game.setRank);*/
    	}
        
    }
    Last edited by JosAH; 06-06-2013 at 05:44 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cannt find symbol - how do I fix this/why isn't it working?

    Your indentation sucks: the play() method is a separate method and it doesn't have a 'game' variable defined; there is one in your main( ... ) method but it doesn't exist in your play() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Cannt find symbol - how do I fix this/why isn't it working?

    Okay - so indentation - how do I make it not suck? What's the proper way to indent?

    I'm REALLY new to this, trying out my first program, and am failing, I am suppose to know what I'm doing after reading what I've read, but it looks like Java isn't coming easily to me.

    If I've got it defined in my main(), how do I define it in the play()?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: Cannt find symbol - how do I fix this/why isn't it working?

    Hm, magically the indentation looks a bit better now (the play() method neaty lines up with the main( ... ) method). Your play() method belongs to a particular Card object (it isn't static), so for starters leave out the 'game.' part in that method so it prints out itself and see if that is what you want; you have to call it as 'game.play();' in your main( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Cannt find symbol - how do I fix this/why isn't it working?

    I've been working on this and I'm trying to get this to work - I've got the PickTwoCards to execute, but it's pulling data from the Card() method instead of the main()method . I am also having a hard time getting the Math.Random into this. Any words of wisdom?

    What I want is to cards to be pulled - I can name the cards, but the numbers need to be random.

    I feel like I shouldn't need the secondSuit - I should be able to set it in the main() to whatever I want right? Should just be two variables with the get and set methods for it, one variable String, one variable int (I need the Math.Random code in with this one)

    Card class:
    Java Code:
    public class Card {
        private String firstSuit;
    	private int rankOfCard;
    	
    
    	public Card(){
            firstSuit = "";
    		rankOfCard = 0;
    	
        }
    	/**
    	* @return the firstSuit
    	*/
    	public String getFirstSuit() {
    		return firstSuit;
    	}
    
    	/**
    	* @param firstSuit the firstSuit to set
    	*/
    	public void setFirstSuit(String firstSuit) {
    		this.firstSuit = firstSuit;
    	}
    		
    	/**
         * @return the rankOfCard
         */
        public int getRankOfCard() {
            return rankOfCard;
        }
    
        /**
         * @param rankOfCard the rankOfCard to set
         */
        public void setRankOfCard(int rankOfCard) {
            this.rankOfCard = rankOfCard;
        }
        
    	public void main(String[] args){
    		//Make it a game;
            Card game = new Card();
    		game.setFirstSuit("Spades");
    		game.setRankOfCard(0);
    		}	
    		
    	public void play(){
    		Card game = new Card();
    		System.out.println("Your first card is a " + getRankOfCard() + " of " + getFirstSuit());
    		game.setFirstSuit("Spades");
    		System.out.println("Your second card is a " + getRankOfCard() + " of " + getFirstSuit());
    		game.setFirstSuit("Hearts");
    		}
    }
    When I execute this class:
    Java Code:
    public class PickTwoCards{
    
    	public static void main(String[] args) {
    	Card game = new Card();
    	game.play();
    	}
    }
    It works, but it gives me:
    Your first card is a 0 of
    Your second card is a 0 of
    Which means it is pulling from the public Card() method instead of the main()

    Help, please I really don't know what I'm missing here.
    Last edited by cheesegraterish; 06-06-2013 at 09:36 PM.

Similar Threads

  1. cannot find symbol
    By vastrolorde in forum New To Java
    Replies: 11
    Last Post: 01-04-2013, 11:11 AM
  2. Cannot find symbol
    By sam.jj12 in forum AWT / Swing
    Replies: 2
    Last Post: 09-16-2012, 01:27 PM
  3. Still cannot find symbol!
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 04:32 PM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •