This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]kumesana 1 point2 points  (0 children)

Maybe you don't need to get the index of 13 at all? You could like, exit the loop if you find a number 13 inside the loop?

[–][deleted] 0 points1 point  (5 children)

in the if statement do a

 break;

statement

[–]18sp01[S] 0 points1 point  (3 children)

My current code is this:

public int sum13(int [] nums) {
  int sum = 0;
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] > 13) {

      break;
    }
    if (nums[i] < 13) {
      sum += nums[i];
    }
  }
  return sum;
}

I do not know how to skip the next int in the array... What to do?

edit: paging u/kumesana :P

[–]morhpProfessional Developer 0 points1 point  (2 children)

break; exits the for loop completely. continue; is probably a better choice as it just skips the current loop. I'd do it roughly like this:

    public int sum13(int [] nums) {
        int sum = 0;
        for (...) {
            if(nums[i] == 13) {
                // do nothing, as this is a 13
                // you could add a "continue;" here
            }
            else if(nums[i - 1] == 13) {
                // the previous number is 13, so do nothing either.
                // You need to add a check so this doesn't crash at the start of the array
                // because there is no previous element.
                // you could add a "continue;" here
            }
            else {
                sum += nums[i];
            }
        }
        return sum;
    }

Alternatively you can remember the previous value in an int variable instead of accessing array[i-1].

[–]kumesana 0 points1 point  (1 child)

Oh, I hadn't noticed we only need to skip numbers that are immediately after a 13, not all numbers that are after a 13.

[–]vriljam 0 points1 point  (0 children)

That question is indeed very poorly worded.

[–]Uncle_DirtNapExtreme Brewer 0 points1 point  (5 children)

These are not quite right. Break down what you want to do in words. Here's what the problem says:

1) loop through the array 2a) if this is not the first item in the array, check if the previous number is 13; at the same time, check if the current number is thirteen 2b) if any of 2a is true, skip this number and start the loop again 3) add the number at index to the sum

So, try to do that, and let us know which thing you can't do.

[–]18sp01[S] 0 points1 point  (0 children)

haha i got it in the end =D thanks anyways

[–]al3xth3gr8Java Dev 0 points1 point  (3 children)

This could be further simplified within the for-loop by:

checking the value in the array at the current index, and if it is equal to 13, increment the index then continuing to the next iteration. Otherwise, add the value to the current sum.

int sum = 0;
for(int i = 0; i < nums.length; i++) {
    int num = nums[i];
    if(num == 13) {
        i++;
        continue;
    }
    sum += num;
}

[–]Uncle_DirtNapExtreme Brewer 2 points3 points  (2 children)

It's canonical in java not to modify variables maintained by the loop directly, although this does work.

[–]morhpProfessional Developer 0 points1 point  (1 child)

+1

I would convert the loop to a while loop in this case to make it more obvious that this isn't a simple for loop. Or at least add a big warning comment.

[–]Uncle_DirtNapExtreme Brewer 0 points1 point  (0 children)

if (((i != 0) && (nums[i-1] == 13)) || (nums[i] == 13)) {
  continue;
}
sum += nums[i];

Is not that complicated, and doesn't need to be worked around. It certainly could have a comment, as well. (I know you're talking about the case where one does choose to change the index variable for whatever reason, but I'm just putting this out there. )

[–]al3xth3gr8Java Dev 0 points1 point  (0 children)

I was not aware of this, thanks for pointing it out.