Scenario:
I need to read an array of integers from the standard inputstream, reorder it in such a way that a prime number is always followed by two even numbers ensuring that the prime and even numbers are picked up from the array in the order in which they are present to build the prime-2even set. Any remaining numbers that can't be part of the set can be placed at the end of the array in the order in which they appear. The input(in multiple lines) and expected output(in multiple lines) as below:
Input:
8 5 9 7 8 5 4 6 8
Expected output:
5 8 4 7 6 8 9 5
Attempt:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalElements = Integer.parseInt(scanner.nextLine());
Integer[] integers = new Integer[totalElements];
Integer[] primeTwoEvens = new Integer[totalElements];
Integer[] leftOvers = new Integer[totalElements];
for (int i = 0; i < integers.length; i++) {
integers[i] = scanner.nextInt();
}
// LOGIC
int notNullCounter = 0; // Variable to track how many elements of integers array has been set to null.
while (notNullCounter != integers.length) { // Repeat this process until all the elements of the integers array are null.
for (int i = 0; i < integers.length; i++) { // Iterate the integers array and check for primeTwoEvens and populate the primeTwoEvens array.
if (integers[i] != null) { // Is the element of integers array to be processed null? If no, proceed.
if (isPrime(integers[i])) { // Is the element of integers array prime? If yes, proceed.
System.out.println(integers[i] + " is prime..."); // Print statement for debugging purpose.
primeTwoEvens[i] = integers[i]; // Since the element of integers array is prime, add it to the primeTwoEvens array.
integers[i] = null; // Set this index of integers array to null.
notNullCounter++; // As one element of integers array has been set to null, increment the null counter.
int evenCounter = 0; // Variable to track even number occurrences.
while (evenCounter != 2) { // Repeat this process until 2 even numbers are not found.
for (int j = ++i; j <= integers.length; j++) { // Iterate the remaining elements of integers array and check for next two even numbers.
if (isEven(integers[j])) { // Is the element of integers array even? If yes, proceed.
System.out.println(integers[j] + " is even..."); // Print statement for debugging purpose.
evenCounter++; // Since the element of integers array is even, increment the even counter.
primeTwoEvens[++i] = integers[j]; // Since the element of integers array is even, add it to the primeTwoEvens array as well.
integers[j] = null; // Set this index of integers array to null.
notNullCounter++; // As one element of integers array has been set to null, increment the null counter.
}
}
}
} /*else { // Element is not prime.
}*/
}
}
//break;
}// End of while
/*System.out.println("@@@@@@@@@@@@ PRINTING THE FINAL SORTED ARRAY @@@@@@@@@@@@");
for (Integer integer : integers) {
System.out.println(integer);
}*/
}
throws me an java.lang.ArrayIndexOutOfBoundsException
Output:
5 is prime...
8 is even...
4 is even...
6 is even...
8 is even...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at Ideone.main(Ideone.java:125)
Line 125:
if (isEven(integers[j]))
Note: I am restricted to using Java standard API and JDK 7.
How do I solve the problem and the final task?