title: Java: Two Scanners reading from the same input file. Doable? Useful?
body: <p>I have to read in integers from an input file based on whether or not a string that appears before them is a certain keyword "load". There is no key number telling how many numbers are about to be inputted. These numbers must be saved to an array. In order to avoid creating and updating a new array for each additional number scanned, I'd like to use a second scanner to first find the amount of integers, and then have the first scanner scan that many times before reverting back to testing for strings. My code:</p>
  
  <pre><code>public static void main(String[] args) throws FileNotFoundException{
      File fileName = new File("heapops.txt");
      Scanner scanner = new Scanner(fileName);
      Scanner loadScan = new Scanner(fileName);
      String nextInput;
      int i = 0, j = 0;
      while(scanner.hasNextLine())
      {
          nextInput = scanner.next();
          System.out.println(nextInput);
          if(nextInput.equals("load"))
          {
              loadScan = scanner;
  
              nextInput = loadScan.next();
              while(isInteger(nextInput)){
                  i++;
                  nextInput = loadScan.next();
              }
  
              int heap[] = new int[i];
              for(j = 0; j &lt; i; j++){
                  nextInput = scanner.next();
                  System.out.println(nextInput);
                  heap[j] = Integer.parseInt(nextInput);
                  System.out.print(" " + heap[j]);
              }
          }
  
  
  
  
      }
  
      scanner.close();
  }
  </code></pre>
  
  <p>My problem seems to be that scanning via loadscan, the secondary scanner only meant for integers, also moves the primary scanner forward. Is there any way to stop this from happening? Any way to make the compiler treat scanner and loadscan as separate objects despite them preforming the same task?</p>