trying to iterate through an array list with a for loop and initialized a variable p in the loop. when using the variable as an index to get from the arraylist it is giving me a p cannot be resolved to a variable.
for (int p = 0; p < this.playerList.size(); p );
Player tempPlayer = this.playerList.get(p);
StandardCard[] tempHoleCards = {gameDeck.getNextCard(), gameDeck.getNextCard()};
tempPlayer.setHoleCards(tempHoleCards);
The variable p is already initialized in the loop but cannot be found when using it for the get method for the array list
CodePudding user response:
Try removing the semicolon and replacing it with brackets:
for (int p = 0; p < this.playerList.size(); p ){
Player tempPlayer = this.playerList.get(p);
StandardCard[] tempHoleCards = {gameDeck.getNextCard(), gameDeck.getNextCard()};
tempPlayer.setHoleCards(tempHoleCards);
}
CodePudding user response:
You have a for loop with no body. Remove the ;
at the end of the for loop.
Also, your intention is to have the three statements within the body of the for loop. So, you should enclose them within braces.
for (int p = 0; p < this.playerList.size(); p ) {
Player tempPlayer = this.playerList.get(p);
StandardCard[] tempHoleCards = {gameDeck.getNextCard(), gameDeck.getNextCard()};
tempPlayer.setHoleCards(tempHoleCards);
....
}
CodePudding user response:
The contents of the loop need to be in {}. So this should work:
for (int p = 0; p < this.playerList.size(); p ) {
Player tempPlayer = this.playerList.get(p);
StandardCard[] tempHoleCards = {gameDeck.getNextCard(), gameDeck.getNextCard()};
tempPlayer.setHoleCards(tempHoleCards);
}