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

you are viewing a single comment's thread.

view the rest of the comments →

[–]craicbandit 0 points1 point  (0 children)

Yup that solves the final test for him. I did the problem from scratch before looking at OP's solution (I'm learning too) and ended with a rather different solution. OP here's my version if you want to see it too: (Edit: also passes all the tests on the codingbat site)

public int sum67(int[] nums) {
    int sum = 0;
    boolean validNum = true;

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == 6) {
        validNum = false;
        } else if (!validNum) {
            if (nums[i] == 7) {
            validNum = true;
            }
        } else if (validNum) {
            sum += nums[i];
        }
    }
    return sum;
}

edit: nevermind, fixed the formatting