The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

java noob: array required but __ found & cannot find symbol

Shazkar ShadowstormShazkar Shadowstorm Registered User regular
edited March 2007 in Help / Advice Forum
Relatively new to programming in general... I need help:

I'm trying to make a program with a deck of cards (note, program has a lot of stuff to go that is unfinished but I'm just trying to work bit by bit and make things work)... and there are two lines that I can't figure out, with the compiler telling me:

Driver.java:35: cannot find symbol
symbol : variable length
location: class Hand
for (int count = 0; count < theHand.length; count++){

and

Driver.java:36: array required, but Hand found
System.out.println("The "+ theHand[count] +".");

Basically for some reason I think the program is treating theHand, which is a "Hand" class, which is an array, as something else, but I dunno why or how.

So I guess I'll post most of my code, I dunno what else to do:
Drive with the problems (some stuff cut out):
public class Driver {
   
    //----------------------------------------
    // Runs estimates of the probability of
    // given card combinations by dealing
    // hands of 7 cards and checking for card
    // combinations (repeating many times).
    //----------------------------------------
    public static void main(String[] args) {
        // Create a Hand, and declare variables
        // to count card combinations and keep
        // track of trials.
        
        Hand theHand = new Hand();
        
        int threeOfAKind = 0;
        int fourOfAKind = 0;
        int runOfFour = 0;
        final int MAX_SHUFFLE = 5;
        final int MAX_TRIALS = 100000;
        
        // Testing shuffle method:
        for (int trials = 1; trials <= MAX_SHUFFLE; trials++) {
            theHand.reset();
            theHand.fillHand();
            System.out.println("Here is a hand after shuffle # "+trials+":");
            for (int count = 0; count < theHand.length; count++){
                System.out.println("The "+ theHand[count] +".");
            }

        }
}

The relevant methods of my Hand class:
public class Hand {
    
    private Card[] hand;
    private Deck theDeck;
    //----------------------------------
    // Creates a new instance of Hand,
    // with no cards in it.
    //----------------------------------
    public Hand() {
        hand = new Card[7];
        theDeck = new Deck();
    }
    
    //----------------------------------
    // Fills hand with 7 cards from the
    // top of the deck.
    //----------------------------------
    public void fillHand() {
        int index = 0;
        while (index < 7) {
            hand[index] = theDeck.deal();
            index++;   
        }
    }
    
    //----------------------------------
    // Empties the hand/resets the hand
    // and reshuffles the deck.
    //----------------------------------
    public void reset() {
        hand = new Card[7];
        theDeck.shuffle();
    }
}


Deck:
public class Deck {
    
    private Card[] deck;
    private int dealIndex;
    
    //----------------------------------
    // Creates a new instance of Deck,
    // and shuffles it.
    //----------------------------------
    public Deck() {
        deck = new Card[52];
        dealIndex = 0;
        
        int index = 0;
        for (int suit = 1; suit < 5; suit++)
            for (int rank = 1; rank <14; rank++){
                deck [index] = new Card(rank, suit);
                index++;
            }
        shuffle();
    }
    
    //-----------------------------------
    // Shuffles the deck so the order of 
    // cards is random.
    //-----------------------------------
    public void shuffle() {
        int count = 0;
        
        while (count <= 250) {
            // Picks 2 random index numbers from 0 to 51
            int randomIndexOne = (int) (52*Math.random());
            int randomIndexTwo = (int) (52*Math.random());
            
            // Switch the two cards at the random indices.
            Card tempCard = deck[randomIndexOne];
            deck[randomIndexOne] = deck [randomIndexTwo];
            deck[randomIndexTwo] = tempCard;
            count++;
            
            // Set the dealIndex to 0 so that when you
            // deal you deal from the top of the reshuffled
            // deck.
            dealIndex = 0;
        }
    }
    
    //-----------------------------------
    // Deals a card from the deck, from
    // the "top" down.
    //-----------------------------------
    
    public Card deal() {
        // Ensure you're not at the bottom of the deck
        if (dealIndex == 51) {
            shuffle();
        }
        // Return the card in the deck that corresponds
        // to the dealIndex, and up the dealIndex so the
        // next time you deal it is the next card.
        
        dealIndex++;     
        return deck[dealIndex-1];
    }
    
}


Card (maybe the problem has to do with how I pass the card to string)?
public class Card {
    
    // Declare the two variables that define
    // each card.
    
    private int suit;
    private int rank;
    
    // Assign numerical values to the 
    //names of suits for organization purposes.
    private final int CLUBS = 1, 
                      DIAMONDS = 2, 
                      HEARTS = 3, 
                      SPADES = 4;
    
    //----------------------------------------------
    // Creates a new instance of Card, with
    // the specified integer input values for
    // rank (1-13) and suit (0-3).
    // (Ace = 1, Jack = 11, Queen = 12, King = 13)
    //----------------------------------------------
    public Card(int cardRank, int cardSuit) {
        suit = cardSuit;
        rank = cardRank;
    }
    
    
    //-------------------------------------------
    // Returns integer value of rank.
    //-------------------------------------------
    public int getRank() {
        return rank;
    }
    
    //-------------------------------------------
    // Returns integer value of suit.
    //-------------------------------------------
    public int getSuit() {
        return suit;
    }
    
    //---------------------------
    // Returns suit as string.
    //---------------------------
    public String getSuitString() {
        switch (suit) {
            case 1: return "Ace";
            case 2: return "two";
            case 3: return "three";
            case 4: return "four";
            case 5: return "five";
            case 6: return "six";
            case 7: return "seven";
            case 8: return "eight";
            case 9: return "nine";
            case 10: return "ten";
            case 11: return "Jack";
            case 12: return "Queen";
            case 13: return "King";  
            default: return "Invalid Rank";
        }
    } 
    
    //---------------------------
    // Returns rank as string.
    //---------------------------
    public String getRankString() {
         switch (suit) {
            case CLUBS: return "Clubs";
            case DIAMONDS: return "Diamonds";
            case HEARTS: return "Hearts";
            case SPADES: return "Spades";
            default: return "Invalid Suit";            
         }
    } 
    
    //---------------------------------
    // Returns a string for Card.
    //---------------------------------
    public String toString() {
        return getRankString() + "of" + getSuitString();
    }
 
}


Yeah, I don't really understand.... nor do I expect any of you to read all that goddamn code, but I didn't know what part is causing the problem!

Also, just for learning purposes, is this a bad way to organize my program and classes and stuff?

EDIT: SHIT why the hell did it format ridiculously stupidly in the post like that?!?!?!!?

poo
Shazkar Shadowstorm on

Posts

  • JHunzJHunz Registered User regular
    edited March 2007
    I think your problem is that you're trying to use your Hand class in those two lines as if it were a straight array, which it is not.

    JHunz on
    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
  • ProtoProto Registered User regular
    edited March 2007
    yeah, what Jhunz said.

    1.
    .length isn't an attribute of hand


    2.
    Driver.java:36: array required, but Hand found
    System.out.println("The "+ theHand[count] +".");

    theHand[count] will return a Hand, not a string. So I don't think you can use it like that.

    Proto on
    and her knees up on the glove compartment
    took out her barrettes and her hair spilled out like rootbeer
  • Shazkar ShadowstormShazkar Shadowstorm Registered User regular
    edited March 2007
    Okay, well, I guess I'll fix the length thing by just putting in a constant.

    I was confused about the theHand[number] thing, because I assume that a card would be in that spot...? I tried to make a new card like Card tempCard = theHand[number] but that doesn't work...
    EDIT: Well, I believe I fixed this part by making a method in the hand class that passes through the card value given an input index value. Whee ?!

    Shazkar Shadowstorm on
    poo
  • blincolnblincoln Registered User regular
    edited March 2007
    It might be a good idea for you to read up on object oriented programming in general.

    Are you maybe confusing yourself by having a class called Hand which has a class variable called hand? You can't refer to a class variable from outside the class if the class variable is declared private. If you had declared it public, you would refer to the array class variable hand like theHand.hand[someNumber], and theHand.hand.length.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • Shazkar ShadowstormShazkar Shadowstorm Registered User regular
    edited March 2007
    Doh.

    I'm a moron. Well, thanks. I see my sillyness.

    EDIT: Actually, which is better, using a method like I did in my work around to my error, to pass the object through, or making the variable public? For the most part our teacher was telling us to use methods as accessors and mutators for instance/class variables, and keep them private.

    Shazkar Shadowstorm on
    poo
  • Shazkar ShadowstormShazkar Shadowstorm Registered User regular
    edited March 2007
    Also, question, I don't know statistics... but what is the probability of getting a 4 card run out of 7 cards? Any idea so I can know if I'm doing this right?

    Shazkar Shadowstorm on
    poo
  • clsCorwinclsCorwin Registered User regular
    edited March 2007
    EDIT: Actually, which is better, using a method like I did in my work around to my error, to pass the object through, or making the variable public? For the most part our teacher was telling us to use methods as accessors and mutators for instance/class variables, and keep them private.

    Correct. Thats called data-hiding, and is a very good practice to keep. I tend to use the format getX() and setX() in my coding, gets being accessor, which would look like:
    public void getX()
    {
         return x;
    }
    

    And likewise for the setX():
    public void setX(int newX)
    {
         x = newX;
    }
    

    Keep your shit hidden so people can't just fuck with your data through some random fuck up, but allow for methods to be able to change that data.

    clsCorwin on
  • blincolnblincoln Registered User regular
    edited March 2007
    clsCorwin wrote: »
    Keep your shit hidden so people can't just fuck with your data through some random fuck up, but allow for methods to be able to change that data.

    My understanding is that it's not really about people, it's about other parts of your program code. If you're going to change a variable in an object, it's a good idea to do it through a method rather than a public variable because you may need to take some action immediately when that value is changed.

    It also lets you only allow variables to be read, not changed. That's mainly what I use the C# built-in equivalent (Properties) for.

    blincoln on
    Legacy of Kain: The Lost Worlds
    http://www.thelostworlds.net/
  • contagious_dcontagious_d Registered User regular
    edited March 2007
    Also, just for learning purposes, is this a bad way to organize my program and classes and stuff?

    It looks like you get the basic idea of OOP. When I took intro programming classes, a lot of people had trouble wrapping their heads around that.

    contagious_d on
  • PhilodoxPhilodox Registered User regular
    edited March 2007
    You are correct that sticking variables behind accessor/mutator methods is the correct way of doing things. One of the ideas behind Java/OO programmiing is abstraction of implementation.

    Even if getVar() and setVar() may just be simple wrappers for a variable, you can change that variable without changing the rest of the program so long as the public interface stays the same.

    Philodox on
    That's a Freudian mansex if I ever cocked one.
    twinsbanneroq0.jpg
  • clsCorwinclsCorwin Registered User regular
    edited March 2007
    Well, I say people becuse thats what my current teachers preachers, that theres always one nut out there that finds he can change your data and will. Yes it also prevents you from doing it also. I'm more worried about him, than me =)

    clsCorwin on
  • Mr.FragBaitMr.FragBait Registered User regular
    edited March 2007
    For more common practices of using objects, don't forget sun has many tutorials that help you understand object based programming. Some other good examples on how you should think about objects is here.

    The hardest part of learning OO based programming is learning how you can use Objects and treat them as things instead of primitive types. Many of my classmates struggled to find ways to figure out my ComS class's assignments. Something as large as a competition of players playing against each other seems difficult if you try to look at the smallest components it'll need, but is easy when you learn how to build classes efficiently. I wish I copied down my instructor's notes when he did a refresher for our ComS228 class with a top-down structure of different card games, i'd give them to you. All my notes is over generics, which you don't want to deal with yet.

    Mr.FragBait on
Sign In or Register to comment.