package ru.job4j.taskIterator;


import java.util.Iterator;

/**
 * Class for iterator for prime number in array.
 * @author atrifonov.
 * @since 16.08.2017.
 * @version 1.
 */
public class PrimeIt implements Iterator<Integer> {
    /**
     * Internal array for store data.
     */
    private final int[] array;
    /**
     * Index.
     */
    private int position = 0;

    /**
     * Construct PrimeIt.
     * @param array array for iterator.
     */
    public PrimeIt(int[] array) {
        this.array = array;
    }

    @Override
    public boolean hasNext() {
        int i = position;
        boolean hasEven = false;
        while (!hasEven && i < array.length) {
            hasEven = isPrime(array[i++]);
        }
        return hasEven;
    }

    @Override
    public Integer next() {
        int item;
        if(isPrime(array[position])) {
            item = array[position++];
        } else {
            while (!isPrime(array[position])){
                position++;
            }
            item = array[position++];
        }

        return item;
    }

    private boolean isPrime(int number) {
        boolean isPrime = true;
        if(number > 1) {
            for (int i = 2; i < number; i++) {
                if(number % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        } else {
            isPrime = false;
        }
        return isPrime;
    }


}

@@ -52,16 +52,57 @@
 terator;
+%0Aimport java.util.NoSuchElementException;
 %0A%0A/**%0A *
@@ -617,25 +617,72 @@
     
-int i = position;
+boolean hasEven = false;%0A        if(position %3C array.length)   %7B
 %0A   
@@ -678,39 +678,35 @@
 th)   %7B%0A        
-boolean
+   
  hasEven = false
@@ -692,38 +692,68 @@
       hasEven = 
-false;
+isPrime(array%5Bposition%5D);%0A        %7D%0A
 %0A        while (
@@ -764,17 +764,24 @@
 Even && 
-i
+position
  %3C array
@@ -788,19 +788,42 @@
 .length)
- 
 %7B%0A
+            position++;%0A
         
@@ -850,19 +850,24 @@
 e(array%5B
-i++
+position
 %5D);%0A    
@@ -958,51 +958,18 @@
    i
-nt item;%0A        if(isPrime(array%5Bposition%5D
+f(hasNext(
 )) %7B
@@ -973,38 +973,38 @@
 ) %7B%0A            
-item =
+return
  array%5Bposition+
@@ -1040,121 +1040,42 @@
     
-while (!isPrime(array%5Bposition%5D))%7B%0A                position++;%0A            %7D%0A            item = array%5Bposi
+throw new NoSuchElementExcep
 tion
-++%5D
+()
 ;%0A  
@@ -1085,30 +1085,8 @@
    %7D
-%0A%0A        return item;
 %0A   

edit task 22211