Originally Posted by
theoriginalanomaly
If you read through the link Copeg supplied it answers the question. The problem isn't the for loop, it is a problem with your variables.
Yea, I figured out that when I add the Card to the ArrayList, when I update the properties, all the properties are updated. How do I make it so that it will add a new Card every time the loop goes through.
For some reason ".add( new Card(id, suit) );" wasn't working.
--- Update ---
Originally Posted by
copeg
But they do affect the code - there is a reason the compiler issues these warnings
Ok, I took out the suppressing ( the only warnings it was giving me were that the array lists didn't contain any values when initialized). I have turned the loop to look like the following:
Card a = new Card( 0, 0 );
for(int i = 2; i < 15; i++){ // get int 1 through 13
//a = new Card(i, 1); // new Card w/ id=i & suit = 1
a.setID(i);
System.out.println( a.toString() + "~");
deck.add( new Card( i, 0) ); //add new Card to deck
}
System.out.println( deck.size() );
It is supposed to add a new Card to the array in each loop of the for statement, but is still resetting all other Card values.
Result:
2 of UNKNOWN~
3 of UNKNOWN~
4 of UNKNOWN~
5 of UNKNOWN~
6 of UNKNOWN~
7 of UNKNOWN~
8 of UNKNOWN~
9 of UNKNOWN~
10 of UNKNOWN~
Jack of UNKNOWN~
Queen of UNKNOWN~
King of UNKNOWN~
Ace of UNKNOWN~
13
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
--- Update ---
So I went through and put in some debugging output code and this is what the loop looks like:
Card a = new Card( 0, 0 );
System.out.println( "~start #1~");
for(int i = 2; i < 15; i++){ // get int 1 through 13
//a = new Card(i, 1); // new Card w/ id=i & suit = 1
a.setID(i);
System.out.println( (new Card(i, 0)).toString() + "~");
deck.add( new Card( i, 0) ); //add new Card to deck
System.out.println( "~~start #2~~");
for(int j = 0; j < deck.size(); j++){
System.out.println(
deck.get(j).toString()
);
}
System.out.println( "~~end #2~~");
}
System.out.println( "~end #1~" );
System.out.println( "size: " + deck.size() );
And the result:
~start #1~
2 of UNKNOWN~
~~start #2~~
2 of UNKNOWN
~~end #2~~
3 of UNKNOWN~
~~start #2~~
3 of UNKNOWN
3 of UNKNOWN
~~end #2~~
4 of UNKNOWN~
~~start #2~~
4 of UNKNOWN
4 of UNKNOWN
4 of UNKNOWN
~~end #2~~
5 of UNKNOWN~
~~start #2~~
5 of UNKNOWN
5 of UNKNOWN
5 of UNKNOWN
5 of UNKNOWN
~~end #2~~
6 of UNKNOWN~
~~start #2~~
6 of UNKNOWN
6 of UNKNOWN
6 of UNKNOWN
6 of UNKNOWN
6 of UNKNOWN
~~end #2~~
7 of UNKNOWN~
~~start #2~~
7 of UNKNOWN
7 of UNKNOWN
7 of UNKNOWN
7 of UNKNOWN
7 of UNKNOWN
7 of UNKNOWN
~~end #2~~
8 of UNKNOWN~
~~start #2~~
8 of UNKNOWN
8 of UNKNOWN
8 of UNKNOWN
8 of UNKNOWN
8 of UNKNOWN
8 of UNKNOWN
8 of UNKNOWN
~~end #2~~
9 of UNKNOWN~
~~start #2~~
9 of UNKNOWN
9 of UNKNOWN
9 of UNKNOWN
9 of UNKNOWN
9 of UNKNOWN
9 of UNKNOWN
9 of UNKNOWN
9 of UNKNOWN
~~end #2~~
10 of UNKNOWN~
~~start #2~~
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
10 of UNKNOWN
~~end #2~~
Jack of UNKNOWN~
~~start #2~~
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
Jack of UNKNOWN
~~end #2~~
Queen of UNKNOWN~
~~start #2~~
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
Queen of UNKNOWN
~~end #2~~
King of UNKNOWN~
~~start #2~~
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
King of UNKNOWN
~~end #2~~
Ace of UNKNOWN~
~~start #2~~
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
Ace of UNKNOWN
~~end #2~~
~end #1~
size: 13
It has to be rewriting the previous inputs, but I dont know why. I am probably missing something very obvious...
--- Update ---
Originally Posted by
copeg
Oh wow. That was silly. It might have had something to do with me having the wrong definition of static in my head. Haha. Thanks! That solved the problem, now that I actually read the definition of a static modifier! hahaha! I feel really stupid now... Thanks again!
--- Update ---
If anyone stumbles across this, and wants to know that final debugged code:
Deck.java (snippet)
public static ArrayList<Card> deck = new ArrayList();
public Deck(){
/*for(int suit = 1; suit < 5; suit++){
for(int id = 1; id < 15; id++){
//System.out.println( new Card(id, suit).toString() );
deckName.add( new Card(id, suit).toString() );
deck.add( new Card(id, suit) );
}
}*/
for(int suitNum = 1; suitNum < 5; suitNum++){
for(int id = 2; id < 15; id++){ // get int 1 through 13
deck.add( new Card( id, suitNum) ); //add new Card to deck
}
}
System.out.println( "size: " + deck.size() );
}
Card.java
import javax.swing.ImageIcon;
public class Card {//extends JLabel
public int id;
public String suit, name, iconString;
private ImageIcon icon;
public Card(int id, int suitNum){
this.id = id;
suitType(suitNum);
idType(id);
}
private void suitType(int suitNum){
if(suitNum == 1){
suit = "Diamonds";
}else if(suitNum == 2){
suit = "Hearts";
}else if(suitNum == 3){
suit = "Spades";
}else if(suitNum == 4){
suit = "Clubs";
}else{
suit = "UNKNOWN";
}
}
private void idType(int id){
if(id == 11){
name = "Jack" + " of " + suit;
}else if(id == 12){
name = "Queen" + " of " + suit;
}else if(id == 13){
name = "King" + " of " + suit;
}else if(id == 14){
name = "Ace" + " of " + suit;
}else{
name = id + " of " + suit;
}
}
@Override
public String toString(){
return name;
}
public void setID(int id){
this.id = id;
idType(id);
}
public void setSuit(int suitNum){
suitType(suitNum);
}
/** set icon images */
private void iconSet(int id, int suitNum){
/*for(int i = 2; i < 15; i ++){ // id
for(int j = 1; j < 5; j++){ // suit
if(id == i){
}
}
}*/
// set id + suit = icon1.png
iconString = suit + id + ".png"; // suitID = Diamonds2.png
icon = new ImageIcon(getClass().getResource("Resources/" + iconString));
//this.setText(icon);
}
}