0

I have a problem when I run this code I get an array out of bound exception, the thing is that I changed the face array and added "1", and I have no idea how to fix this, can anyone help, sorry if the problem is stupid.

  public class DeckOfCards_Rumenov{
    public static void main(String[] args) {
        String[] s = {
            "Clubs", "Diamonds", "Hearts", "Spades"
        };

        String[] face = {
        "1" ,"2", "3", "4", "5", "6", "7", "8", "9", "10",
        "Jack", "Queen", "King", "Ace"
        };
        String[] ranks = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10", "10", "10", "1 or 11"
        };
        int n = s.length * face.length;
        String[] deck = new String[n];
        for (int i = 0; i < face.length; i++) {
            for (int j = 0; j < s.length; j++) {
                deck[s.length*i+j] = face[i] + ", " + s[j] + ", " + ranks[i];
            }
        }
        for (int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n-i));
            String temp = deck[r];
            deck[r] = deck[i];
            deck[i] = temp;
        }
        for (int i = 0; i < n; i++) {
            System.out.println(deck[i]);
        }
    }

}
Dark Knight
  • 8,218
  • 4
  • 39
  • 58

1 Answers1

1

Your face array does not have the same length as the ranks array. Yet you use the same iterator (i) for both of them, which goes till the end of the larger array (face).

Yamuk
  • 750
  • 8
  • 27