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 return current number and increment index value.
     * @return simple numbers from an array.
     */

    @Override
    public int next() {
        int result = 0;

        result = this.array[this.index];
        if (this.array.length - 1 > this.index) {
            checkNextIndex();
        }

        return result;
    }

    /**
     * Override hasNext() method to check if there are a simple element in array to read.
     * @return true if there are any elements to read, else return false.
     */

    @Override
    public boolean hasNext() {
        boolean isNext = false;

        while (!isNext && this.array.length - 1 > this.index) {
            isNext = isSimpleOrNot() || checkNextIndex();
        }

        return isNext;
    }

    /**
     * Check if next element is simple and increment index.
     * @return true if next element is simple.
     */

    private boolean checkNextIndex() {
        boolean check = true;
        while (this.array.length > this.index) {
            this.index++;
            if (this.array.length > this.index) {
                check = isSimpleOrNot();
                break;
            }

        }
        return check;
    }

    /**
     * Check if umber is simple.
     * @return true if number is simple.
     */

    private boolean isSimpleOrNot() {

        boolean isSimple = true;

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

        for (int i = 2; i <= Math.sqrt(this.array[this.index]); i++) {
            if (this.array[this.index] % i == 0) {
                isSimple = false;
                break;
            }
        }

        return isSimple;
    }
}

@@ -17,16 +17,58 @@
 osyan;%0A%0A
+import java.util.NoSuchElementException;%0A%0A
 /**%0A * E
@@ -615,24 +615,54 @@
 c int next()
+ throws NoSuchElementException
  %7B%0A        i
@@ -676,25 +676,24 @@
 lt = 0;%0A
-%0A
         
 result =
@@ -688,39 +688,111 @@
     
-result = this.array%5Bthis.index%5D
+if (this.index %3E= this.array.length) %7B%0A            throw new NoSuchElementException(%22No simple number%22)
 ;%0A  
@@ -788,35 +788,67 @@
 umber%22);%0A       
+ 
- if
+%7D%0A%0A        try %7B%0A            while
  (this.array.len
@@ -843,36 +843,32 @@
 is.array.length 
-- 1 
 %3E this.index) %7B%0A
@@ -879,31 +879,301 @@
         
+    
-checkNextIndex(
+if (isSimpleOrNot()) %7B%0A                    result = this.array%5Bthis.index++%5D;%0A                    break;%0A                %7D%0A                index++;%0A            %7D%0A%0A        %7D catch (ArrayIndexOutOfBoundsException out) %7B%0A            throw  new NoSuchElementException(%22No such element%22
 );%0A     

change logic and it is work