title: Changing recursive method to iterative
body: <p>i have recrusive function which works fine.  The problem is it gives stackoverflow error when the number of lines are huge.  I want to put it in iterative, probably using a for loop.  Need some help in doing it.    </p>
  
  <pre><code>private TreeSet validate(int curLine, TreeSet errorSet) {
      int increment = 0;
      int nextLine = 0;
  
      if (curLine == lines.length || errorSet.size() != 0) {
          return errorSet;
      } else {
          String line = lines[curLine];
  
          //validation starts.  After validation, line is incremented as per the requirements
  
          increment = 1 //As per requirement. Depends on validation results of the line
  
          if (increment &gt; 0) {
              try{
                  Thread.currentThread().sleep(100);  
              }catch(Exception ex){
                  System.out.println(ex); 
              }
              nextLine = (curLine + increment);
              validate(nextLine, errorSet);
          }
      }
  
      return errorSet;
  } 
  </code></pre>
  
  <p>Poster's description of the method:</p>
  
  <p>The method does validates textlines, these lines has instructions of how much line has to be skipped, if the line is valid. So, if the line is valid that many of lines will be skipped using the increment. if the line is not valid increment will be 0. </p>