title: Java Why does my for loop set i to 0 after i is 4?
body: <p>Ok, I'm trying to get the output to look a certain way. Print 10 numbers the new line. For some reason after running through loop 4 it sets i to 0. I'll put the file I'm reading from first. I eventually want to write it to a new file. This is for a class, just one assignment and I'm not asking you guys to do the assignment. (Basically I have to read the file and write to a new file with the name designated in the first file. And only use the number of numbers at the top of the first file.</p>
  
  <p>The txt file contents:</p>
  
  <pre><code>MyOutput.txt 23 
  20 1 4 5 7 
  45
   1 2 3 4 5 6 7 8 9 77 88 99 23 34 
  56 66 77 88 99 100 110 120
  </code></pre>
  
  <p>and the code
  ...</p>
  
  <pre><code>package reading_with_exceptions;
  
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.util.Scanner;
  
  public class inputFilename {
  
  
      static Scanner kbInput = new Scanner(System.in);
  
      public static void main(String[] args) {
          String lines = null;
          Scanner inFile = null;
          int count = 0;
          boolean metCount = false;
          // TODO Auto-generated method stub
          try {
              inFile = new Scanner(new FileReader("src/MyInput.txt"));
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }   
          lines = inFile.nextLine();
          lines.trim();
  
          //Now check to see if line is empty
          //If not empty get the ouput file name and number of lines from file
          if(lines.length()&gt;0) {        
              String outPutFile = lines.substring(0, lines.indexOf(' '));
              String numNumbers = lines.substring((lines.indexOf(' ')+1), (lines.length()-1));
              numNumbers.trim();
              count = Integer.parseInt(numNumbers);
              System.out.print(outPutFile+"  ");
              System.out.println(count);
          }
  
          //
  
  
          //check if there is another line in the file
          int i=0;
          while(inFile.hasNextLine() &amp;&amp; !metCount) {
  
              lines = inFile.nextLine();
              String[] separated = lines.split(" ");
  
              for( i = 0; i&lt;separated.length; i++) {
  
                  System.out.print(separated[i]+"  ");
  
                  if((i+1) % 10 ==0) {
                      System.out.println("...");
                  }
                  //System.out.print("  spot:"+i+" ");
                  //System.out.println("i:" +i  +"count:"+count);
                  if(i&gt;=count)
                      metCount = true;
  
              }
  
          }
      }
  }
  </code></pre>
  
  <p>...</p>
  
  <p>I've tried stepping though it in Eclipse and still can't resolve the problem. It only happens every other time. First count goes to 4 Second count goes to 10 Third goes to 4 etc.</p>