Given an array full of integers, I need to print a sum that excludes the number 13 and also the number that comes immediately after 13, if there is any. For example:
int[] nums = {1, 1, 13, 1, 13} is supposed to return a sum of 2.
I looped through the array to exclude the 13's from the final sum, but I don't know how to target the index immediately after the index I'm trying to locate (if it exists). I keep running into "Array Index Out of Bounds Exceptions."
public static void main(String[] arguments) {
int[] nums = {1, 1, 13, 1, 13};
int targetNums = 0;
int sum = 0;
int sumFixed = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 13) {
targetNums += nums[i];
}
sum += nums[i];
}
sumFixed = sum - targetNums;
System.out.println(sumFixed);
}