title: Preventing IndexOutOfBoundsException on while loop [Java]
body: <p>I am making a card game and it works well except I cannot get to the bottom of the deck without running into an IndexOutOfBoundsException error at some point. I have an array of numbers between 1 and 52 that are randomized and I use that to choose what index to call from the arrayList of cards I have. Since the deck is an ArrayList I can remove each card after each loop easily. But I cannot remove the number from the random number array and so as the list of cards gets smaller, the bigger chance of calling an index that is too high I get. Can't figure out a way around it. I have tried using an if statement to change the value of the the random number to something less than the max length of the arrayList but still get the error. </p>
  
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
      System.out.println("Welcome to War! Press enter to begin."); //User prompt
      Scanner scanner = new Scanner(System.in); //Scanner init
      String[] suit = {" of Diamonds", " of Spades", " of Hearts", " of Clubs"};//Array for suits
      String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};//Array for faces
      String[] deck = new String[52]; //Deck size setup
      boolean deckComplete = false;//Boolean for deck being finished
      int[] random = new int[52];//Array for all numbers between 0 and the number of cards in the deck multiplied by number of decks
      ArrayList&lt;Integer&gt; random1 = new ArrayList&lt;Integer&gt;();
      int playerOneTotal = 0;
      int playerTwoTotal = 0;
  
  
  
      for (int x = 0; x&lt;random.length; x++) {
          random[x] = x;
      }//Content setup for random array
  
      Random rndNum = new Random();//Random init
  
      int i = 0;
  
          for (i = 0; i &lt; deck.length; i++) {
  
              deck[i] = faces[i % 13] + suit[i % 4];
          }//Deck of cards setup
  
  
  
      ArrayList&lt;String&gt; arrayList = new ArrayList&lt;String&gt;(Arrays.asList(deck));//Changing Array with card contents into an ArrayList
  
      while (deckComplete == false) {         //While loop for dealing cards
  
  
          for (int j = deck.length; j &gt;= 1; j--) {
  
  
             // System.out.println("Hit enter to be dealt a card!");    
              String readString = scanner.nextLine(); //Set variable "readString" to user input
              int randomNumber = rndNum.nextInt(j);
              int randomNumberTwo = rndNum.nextInt(j);
  
  
              if (readString.equals("")) {        //If user input equals "enter"...
  
                   if (randomNumber &gt;= arrayList.size()) {
                      randomNumber = arrayList.size() - 1;
                  }
  
                  if (randomNumberTwo == arrayList.size()) {
                      randomNumberTwo = arrayList.size() - 1;
                  }
  
  
                    int playerOne = random[randomNumber];
                    int playerTwo = random[randomNumberTwo];
                    int playerOneScore = 0;
                    int playerTwoScore = 0;
  
  
                    System.out.println("Player One Draws a: " + arrayList.get(random[randomNumber])); //Print out card
                    arrayList.remove(random[randomNumber]); //Remove card from deck
                    System.out.println("Player Two Draws a: " + arrayList.get(random[randomNumberTwo])); //Print out card
                    arrayList.remove(random[randomNumberTwo]); //Remove card from deck
  
  
                    playerOneTotal = playerOneTotal + playerOneScore;
                    playerTwoTotal = playerTwoTotal + playerTwoScore;
  
                    if (playerOneTotal &gt; playerTwoTotal) {
                        System.out.println("Player One Wins this round! Current Score - Player One: " + playerOneTotal + " Player Two: " + playerTwoTotal);
                    }else if (playerOneTotal &lt; playerTwoTotal) {
                        System.out.println("Player Two Wins this round! Current Score - Player One: " + playerOneTotal + " Player Two: " + playerTwoTotal);
                    }else if (playerOneTotal == playerTwoTotal) {
  
                    }
  
  
                     if (j == 1) {    //If program gets to last card...
                         deckComplete = true; //Set desk complete to true
  
                         System.out.println(arrayList.get(random[randomNumber])); //Print last card
                         arrayList.remove(random[randomNumber]); //Remove last card
                         System.out.println("You are out of cards!"); //Print "You are out of cards"
                         break; //Stop loop
                     }  
                 }
  
  
  
         }
  </code></pre>
  
  <p>Is there any method besides trying (below) that would get me the right outcome?</p>
  
  <pre><code>if (randomNumber &gt;= arrayList.size()) {
                      randomNumber = arrayList.size() - 1;
                  }
  
                  if (randomNumberTwo == arrayList.size()) {
                      randomNumberTwo = arrayList.size() - 1;
                  }
  </code></pre>
  
  <p>Edit: Here is the exact error message. </p>
  
  <p><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 33, Size: 23
      at java.util.ArrayList.rangeCheck(ArrayList.java:635)
      at java.util.ArrayList.get(ArrayList.java:411)
      at Game.main(Game.java:72)</code></p>