title: Populating 2D array from text file using scanner Java
body: <p>I am trying to populate a 2D array in Java through reading a text file using scanner. Currently, I don't fully understand what's going wrong. Below are the text file I'm using, the 2D array when it is printed and my code.</p>
  
  <p><strong>Text File</strong></p>
  
  <pre><code>30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 28 22 23 29 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 28 22 33 9 10 34 23 29 30 30 30 30 30 30 
  30 30 30 30 28 22 33 9 17 1 1 18 10 34 23 29 30 30 30 30 
  30 30 28 22 33 9 17 1 1 1 1 1 1 18 10 34 23 29 30 30 
  28 22 33 9 17 1 1 1 1 1 1 1 1 1 1 18 10 34 23 29 
  33 9 17 1 1 2 1 1 1 1 1 1 1 1 6 1 1 18 10 34 
  32 8 16 1 1 1 1 1 1 1 1 1 1 1 1 5 1 15 7 31 
  27 25 32 8 16 1 1 3 1 1 1 1 1 1 1 15 7 31 24 26 
  30 30 27 25 32 8 16 1 1 1 1 1 1 15 7 31 24 26 30 30 
  30 30 30 30 27 25 32 8 16 1 1 15 7 31 24 26 30 30 30 30 
  30 30 30 30 30 30 27 25 32 8 7 31 24 26 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 27 25 24 26 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
  30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
  </code></pre>
  
  <p><strong>Printed Array</strong></p>
  
  <pre><code>Map array: [[30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 6, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 30, 1, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 28, 10, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 32, 24, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 16, 30, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 17, 1, 1, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 23, 1, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 7, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 33, 17, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 1, 1, 1, 27, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
  </code></pre>
  
  <p>Currently, my code reads every integer in a while loop. Whilst the x-coordinate (The x position in the array) increases up to 19 in the array, it is set to zero and the y-coordinate increases (The y position in the array). This is the method I used to populate the array.</p>
  
  <p>Note: The width and height of 2D array (mapWidth and mapHeight) and text file are both 20 * 20. Additionally, the initial value of xCoord and yCoord are 0.</p>
  
  <p><strong>My Code</strong></p>
  
  <pre><code>while (fileScannerTileMap.hasNext()) {
  
      if (fileScannerTileMap.hasNextInt()) {
          tileMap[xCoord][yCoord] = fileScannerTileMap.nextInt();
      }
  
      fileScannerTileMap.next();
  
      // Increase xCoord each time
      xCoord++;
  
      // When xCoord reaches its max at 19, increase yCoord and set xCoord equal to zero
      if (mapWidth - 1 == xCoord) { 
          yCoord++;
          xCoord = 0;
      }
  }
  </code></pre>
  
  <p>Any help would be appreciated! </p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>I want to create a 2D array that matches the text file so I can later reference the values of the 2D array easily in a sprite sheet to create terrain for a 2D game.</p>
  
  <p>So Text File --> 2D array --> Terrain Generation  </p>