package ru.matevosyan;

/**
 * EvenNumber class.
 * Created on 03.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class NaturalNumber implements NaturalNumberIterator {

    private int index = 0;
    private final int[] array;

    /**
     * EvenNumber constructor.
     * @param array an array.
     */

    public NaturalNumber (int[] array) {
        this.array = array;
    }

    /**
     * Override the next() method to iterate each element from an array.
     * @return even numbers from an array.
     */

    @Override
    public int next() {
//
//        for (int i = index++; i < array.length && index <= array.length; i++) {
////            int num = array[i];
//            if ((array[i] > 1) && !(array[i] % 2 == 0) || (array[i] == 2)) {
//                value = i;
//                break;
//            } else {
//                index++;
//            }
//        }

        int result = 0;

            if (isSimpleOrNot() && this.array.length > this.index) {
                result = this.array[this.index];
this.index++;
            }

        return result;

    }

    /**
     * Override hasNext() method to check if eny of elements are in array to read, looking to next() pointer.
     * @return true if there are any elements to read, else return false.
     */

    @Override
    public boolean hasNext() {
        return this.array.length > this.index && (isSimpleOrNot()|| this.array.length > this.index);
    }

    private boolean isSimpleOrNot() {
        boolean isSimple = true;

        if (this.array[this.index] == 1) {
            this.index++;
            isSimple = false;

        }

        for (int i = 2; i <= Math.sqrt(this.array[this.index]); i++) {


            if (this.array[this.index] % i == 0) {
//                this.index++;
                isSimple = false;
                break;
            } else  {
                this.index++;
            }

        }

        return isSimple;
    }
}

@@ -569,341 +569,8 @@
 ) %7B%0A
-//%0A//        for (int i = index++; i %3C array.length && index %3C= array.length; i++) %7B%0A////            int num = array%5Bi%5D;%0A//            if ((array%5Bi%5D %3E 1) && !(array%5Bi%5D %25 2 == 0) %7C%7C (array%5Bi%5D == 2)) %7B%0A//                value = i;%0A//                break;%0A//            %7D else %7B%0A//                index++;%0A//            %7D%0A//        %7D%0A%0A
     
@@ -626,41 +626,27 @@
 t() 
-&& this.array.length %3E this.i
+%7C%7C checkNextI
 ndex
+()
 ) %7B%0A
@@ -694,28 +694,48 @@
 index%5D;%0A
-this.i
+                checkNextI
 ndex
-++
+()
 ;%0A      
@@ -1092,39 +1092,337 @@
 )%7C%7C 
-this.array.length %3E this.index)
+checkNextIndex());%0A    %7D%0A%0A    private boolean checkNextIndex() %7B%0A        boolean check = true;%0A        while (this.array.length %3E this.index) %7B%0A            this.index++;%0A            if (this.array.length %3E this.index) %7B%0A                check = isSimpleOrNot();%0A                break;%0A            %7D%0A%0A        %7D%0A        return check
 ;%0A  
@@ -1460,24 +1460,25 @@
 leOrNot() %7B%0A
+%0A
         bool
@@ -1546,34 +1546,8 @@
 ) %7B%0A
-            this.index++;%0A
     
@@ -1572,17 +1572,16 @@
  false;%0A
-%0A
         
@@ -1654,18 +1654,16 @@
  i++) %7B%0A
-%0A%0A
         
@@ -1709,40 +1709,8 @@
 ) %7B%0A
-//                this.index++;%0A
     
@@ -1779,61 +1779,8 @@
    %7D
- else  %7B%0A                this.index++;%0A            %7D%0A
 %0A   

change logic