Changed if statment to this and seems to work fine.
if (i + 1 == arr2.size()) { result = result + arr2.get(i); } else if (arr2.get(i) % 2 == 0 && i < arr2.size() && arr2.get(i + 1) % 2 ==0) { result = result + arr2.get(i) + "-"; } else{ result = result + arr2.get(i); }
I think this is because the else statement is now taking care of the out of bounds, and the previous conditions everything in between. Can anyone shed any light.