title: Java NoSuchElementException using scanner.nextInt()
body: <p>I am trying to read in a pgm file (512x512 array) and when I read in a larger file I get the error: <em>java.util.NoSuchElementException</em> on reading element (3,97).  </p>
  
  <p>I have created a much smaller file to read (23x23) and it reads fine. Is there a size limit? I have checked the file and confirmed that there is an int for the value:
  This appears to be the line it crashes at:</p>
  
  <pre><code>fileArray[row][col] = scan.nextInt();
  </code></pre>
  
  <p>Here is the file:</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  public class FileReader {
      public static void main(String[] args) throws IOException {
  
          String fileName = "lena.pgma";
          int width, height, maxValue;
          FileInputStream fileInputStream = null;
  
          fileInputStream = new FileInputStream(fileName);
          Scanner scan = new Scanner(fileInputStream);
          // Discard the magic number
          scan.nextLine();
          // Discard the comment line
          scan.nextLine();
          // Read pic width, height and max value
          width = scan.nextInt();
          System.out.println("Width: " + width);
  
          height = scan.nextInt();
          System.out.println("Heigth: " + height);
          maxValue = scan.nextInt();
  
          fileInputStream.close();
  
           // Now parse the file as binary data
           FileInputStream fin = new FileInputStream(fileName);
           DataInputStream dis = new DataInputStream(fin);
  
           // look for 4 lines (i.e.: the header) and discard them
           int numnewlines = 4;
           while (numnewlines &gt; 0) {
               char c;
               do {
                   c = (char)(dis.readUnsignedByte());
               } while (c != '\n');
               numnewlines--;
           }
  
          // read the image data
           int[][] fileArray = new int[height][width];
  
           for (int row = 0; row &lt; height; row++) {
               for (int col = 0; col &lt; width; col++) {
  
                   fileArray[row][col] = scan.nextInt();
                   System.out.print("(" + row + " ," + col +"): " + fileArray[row][col]+ " ");
  
               }
  
               System.out.println();
           }
           dis.close();
  
  
      }
  
  }
  </code></pre>
  
  <p>any advise would be appreciated.</p>