Reputation: 51
I've been trying to create a card application to complete a perfect shuffle. I am being met with the error above within this segment of code.
public class Deck {
private List<Card> cards;
private int size;//number of cards in deck
public Deck(String[] faces, String[] suits, int[] values){
cards = new ArrayList<Card>();
for(String suit: suits)
for(int value = 0; value < values.length; value++){
Card a = new Card(faces[face], suit, values[face]);
cards.add(a);
}
size = cards.size();
}//ends deck
More specifically this line, in reference to the face variable.
Card a = new Card(faces[face], suit, values[face]);
I have created the variable face in another class i have stored in the application called Card.
public class Card {
private String suit;
private String face;
private int value;
public Card(String cardFace, String cardSuit, int cardValue){
face = cardFace;
suit = cardSuit;
value = cardValue;
}//ends Card
public String suit(){
return suit;
}//ends suit
public String face(){
return face;
}//ends face
Im sure this is an easy fix just looking a point in the right direction, thanks.
Upvotes: 1
Views: 108
Reputation: 79550
Replace
for(int value = 0; value < values.length; value++){
Card a = new Card(faces[face], suit, values[face]);
cards.add(a);
}
with
for(int value = 0; value < values.length && value < faces.length; value++){
Card a = new Card(faces[value], suit, values[value]);
cards.add(a);
}
The following issues have been addressed in the code given above:
value
as the index value.values
and faces
arrays because the values from each of these arrays are being accessed using the loop counter, value
.Upvotes: 1
Reputation: 109
Card a = new Card(faces[face], suit, values[face]);
face is not defined in faces[face] & values[face]
Upvotes: 0