View Single Post
Old 04-27-2012, 04:06 PM   PM User | #1
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
Getting object vars after retrieval from vector

I just started out trying to make a simple card game as of last night.

My problem is on line 16 and 17 of the first file. I have successfully created the 52 objects with variables for the cards, and once created they are placed into a Vector (the deck). Just to test it worked I used the below code, but the name is underlined and I get the error name cannot be resolved or is not a field.

Does anyone know why this may be? The objects are deffinately in the Vector, I just can't get to the variables they contain.

Thanks.

Code:
Object ob = (Object) Cards.deck.get(i);
System.out.println(ob.name);
****head.java
PHP Code:
import javax.swing.JOptionPane;

public class ****
head {
    
    public ****
head() {
        
        
int p 0;
        for (
int i 0Cards.allCards.lengthi++) {
            
!= 13 0;
            
Cards card = new Cards();
            
            
card.name Cards.allCards[i];
            
card.cardValue 1;
            
card.imgPath "images\\cards\\" card.name ".gif";
            
Cards.deck.add(card);
            
Object ob = (Object) Cards.deck.get(i);
            
System.out.println(ob.name);
            
p++;
            }
        }
    
    
    public static 
void main(String[] args) {
        ****
head sh = new ****head();
        
    }

Cards.java
PHP Code:
import java.util.Vector;

public class 
Cards {
    
    static 
String[] allCards = { 
        
"A-Hearts""2-Hearts""3-Hearts""4-Hearts""5-Hearts"
        
"6-Hearts""7-Hearts""8-Hearts""9-Hearts""10-Hearts"
        
"J-Hearts""Q-Hearts""K-Hearts",
    
        
"A-Dimonds""2-Dimonds""3-Dimonds""4-Dimonds""5-Dimonds"
        
"6-Dimonds""7-Dimonds""8-Dimonds""9-Dimonds""10-Dimonds"
        
"J-Dimonds""Q-Dimonds""K-Dimonds",
    
        
"A-Clubs""2-Clubs""3-Clubs""4-Clubs""5-Clubs"
        
"6-Clubs""7-Clubs""8-Clubs""9-Clubs""10-Clubs"
        
"J-Clubs""Q-Clubs""K-Clubs",
    
        
"A-Spades""2-Spades""3-Spades""4-Spades""5-Spades"
        
"6-Spades""7-Spades""8-Spades""9-Spades""10-Spades"
        
"J-Spades""Q-Spades""K-Spades" 
        
};
    
    
    static 
String name;
    static 
int cardValue;
    static 
String imgPath;
    
    static 
Vector <Objectdeck = new Vector<Object>(allCards.length);
    
    public 
Cards() { }
    


Last edited by dan-dan; 04-27-2012 at 04:20 PM..
dan-dan is offline   Reply With Quote