0

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);
}
3
  • 1
    But this isn't the code producing an OutOfBoundsException, is it? Commented Aug 12, 2015 at 22:22
  • @SebastianS Yeah, it isn't. I took out that portion of the code. Commented Aug 12, 2015 at 22:25
  • The current accepted answer doesn't add up to 2. Commented Aug 13, 2015 at 1:45

4 Answers 4

4

The code you have posted won't throw an ArrayIndexOutOfBoundsException. Perhaps you tried something else that did throw that exception. But it doesn't eliminate the number after the 13.

If the current number is 13, add the next number to targetNums, being careful not to go off the end of the array with a length check of the next index.

if (nums[i] == 13) {
    targetNums += nums[i];

    // Added code
    if (i < nums.length - 1)
    {
        targetNums += nums[i + 1];
    }
    // End added code
}
Sign up to request clarification or add additional context in comments.

Comments

1
public static void main(String[] arguments) {

    int[] nums = {1, 1, 13, 1, 13};

    int sum = 0;

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == 13) {
           i++; // skip current and next
        } else {
            sum += nums[i];
        }
    }

    System.out.println(sum);
}

Comments

0
public static void main(String[] arguments) {

int[] nums = {1, 1, 13, 1, 13};

int sumFixed = 0;
boolean foundThirteen = false;

for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 13) {
        foundThirteen = true;
    } else if(!foundThirteen) {
        sumFixed = sumFixed + nums[i];
    }
}
System.out.println(sumFixed);
}

Comments

0

A different approach using an Iterator. So there is no need to deal with indexes or flags.

Iterator<Integer> iter = Arrays.asList(nums).iterator();
while (iter.hasNext()) {
    Integer num = iter.next();
    if (num == 13) {
        targetNums += num;
        if (iter.hasNext()) {
            targetNums += iter.next(); // not sure if you want to add this to targetNums, too...
        }
    } else {
        sum += num;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.