-3

In trying to find to work out a programming puzzle in java, I'm getting some errors that I don't understand even though I've tried editing the code / narrowing the code down to try figure it out.

My entire program is below and can be run as soon as you paste it (main method is written with the current test parameters that I want in the method that I made already). The description of the puzzle is written in the right after the start of the class, but in short the puzzle is to make a method that takes 3 parameters a, b, and c, and the method returns the sum of these 3 unless any of them is the integer 13 - in this case this 13 and the parameters to the right are omitted from the sum.

When I try run my current test it says there is an ArrayIndexOutOfBoundsException: 3 error under the line if(x == 0 && x == 13) which is found under the for loop for(int x=0; x<params.length; x++).

I don't understand how this error is coming about - I'm adding making x the counter for the parameter inside the array, and adding 1 and 2 to it to make the elements on the right 0. I understand it could go out of bounds here but it's under an if statement that checks if the x is 13 and 0, meaning it has to only perform this when I'm checking the first param (at index 0). But for some reason it is still out of bounds, as if the it is doing the check at x>=1 where the +1 and +2 after will send it out of bounds.

Can any one see what's causing the error?

Note: My idea solving this was store the params in the array, check if they are 13, if it is make that number 0 and ones stored to its right 0, then return the sum of the array after.

public class Logic2Q3 {
    /*
     * Given 3 int values, a b c, return their sum. However, if one of the values is 13 
     * then it does not count towards the sum and values to its right do not count. 
     * So for example, if b is 13, then both b and c do not count.
     * 
     * e.g.:
     * luckySum(1, 2, 3) -> 6
     * luckySum(1, 2, 13) -> 3
     * luckySum(1, 13, 3) -> 1
     * 
     */

    public int luckySum(int a, int b, int c) {

        int[] params = new int[3]; // array to store params
        int sum = 0; // for storing the final sum 

        // store params for checking here
        params[0] = a;
        params[1] = b;
        params[2] = c;

        // check the array of parameters and alter them according to specs
        for(int x=0; x<params.length; x++) {
            // in the case 13 is found 
            if(params[x] == 13) {

                // case that it's the first element (a) and is also the 13
                if(x == 0 && x == 13)
                    // make both elements on the right 0 to not add to the sum
                    params[x] = 0; 
                    params[x+1] = 0;                        
                    params[x+2] = 0;  

                // case that it's the second element (b)
                if(x == 1 && x == 13)
                    // make only the element on the right 0
                    params[x] = 0; 
                    params[x+1] = 0;

            }
        }

        // after the array is altered to omit a, b, and c being 13 or to the right of 13, sum everything
        for(int x=0; x<params.length; x++) {
            // += must be used on initialised instance variable only 
            sum += params[x]; 
        }


        return sum; 

    }

    public static void main(String[] args) {

        Logic2Q3 test = new Logic2Q3(); 

        System.out.println(test.luckySum(3,13,7));



    }

}
darkphoton
  • 85
  • 7
  • 2
    you are accessing the index `x+1`, so potentially `2+1`, whilst your array has the size `3`, where the highest index is `2` – SomeJavaGuy Aug 22 '16 at 11:03
  • 2
    `if(x == 0 && x == 13)` How could this be true? `0` is not `13`. If you want to check if the 0th entry equals the 13th entry, it would be `if (params[0]==params[13])`. – khelwood Aug 22 '16 at 11:03
  • Could you attach error/exception stacktrace? – xenteros Aug 22 '16 at 11:03
  • @khelwood the array has the size `3`, so `params[0]==params[13]` would create the same exception – SomeJavaGuy Aug 22 '16 at 11:04
  • you have made it overly complicated. just check elements of array if its not 13 add it to sum and as soon as you encounter 13 just break the loop – Sanjeev Aug 22 '16 at 11:05
  • @KevinEsche Then I withdraw from trying to figure it out. – khelwood Aug 22 '16 at 11:05
  • @KevinEsche is right. I guess you have forgotten some curly brackets after you if conditions. – Florian Aug 22 '16 at 11:06
  • it was supposed to be `if(x == 0 && param[x] == 13)` as the check, in any case I think I over complicated it like some of you have mentioned. I'm still quite new to java sorry – darkphoton Aug 22 '16 at 11:12

2 Answers2

2

the second time your for-loop runs, if param[x] equals 13 or any time later than 0, the line params[x+2] = 0 is always executed! please read a coding guideline for java and never ever use an if-statement without setting the correct scope with {//if statement stuff} !!!

After reading the guidelines you should be able to solve your problem!

EDIT: in addition those if statements can never be true! x cannot equal 0 and 13 at the same time

LuckAss
  • 114
  • 7
1

Whilst your code potentionally creates a IndexOutOfBoundExeption because you do access the values at either x+1 or x+2 there are also other mistakes. There are missing paranthesis and the if condition will never be true, as you check if one value will be equal to two values (unlogical right?).

All in all my best guess is, that you are making this complete luckySum "puzzle" a bit to complicate, because all you actually need is one loop and a variable sum. Once you found a value you can simply break out of the loop and return the sum. Here´s a more simplified version of what you are doing.

public static void main(String[] args) {
    System.out.println(luckySum(1, 2, 3));
    System.out.println(luckySum(1, 2, 13));
    System.out.println(luckySum(1, 13, 3));
}
/**
 * Given 3 int values, a b c, return their sum. However, if one of the values is 13 
 * then it does not count towards the sum and values to its right do not count. 
 * So for example, if b is 13, then both b and c do not count.
 * 
 * e.g.:
 * luckySum(1, 2, 3) -> 6
 * luckySum(1, 2, 13) -> 3
 * luckySum(1, 13, 3) -> 1
 * 
 */
public static int luckySum(int... values) {
    // Using varargs for dynamic amound of values
    // Sum variable
    int sum = 0;
    // loop over all values
    for(int x=0; x<values.length; x++) {
        // in the case 13 is found break out of the loop
        if(values[x] == 13) {
            break;
            // As a second option you could just return sum here
            // return sum;
        }
        // add value to the sum
        sum += values[x];
    }
    return sum;
}

O/P

6
3
1
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • Yes thank you for the answer, I mentioned after that it's supposed to be `if(x == 0 && param[x] == 13)` not `if(x == 0 && x == 13)`. Your version is a lot cleaner, I'll definitely learn from this. I should apologize for over-complicating I'm still quite new to java – darkphoton Aug 22 '16 at 11:40
  • @darkphoton no need to apologize, everyone started at some point. – SomeJavaGuy Aug 22 '16 at 11:41
  • I wanted to ask a couple questions about your code because of how clean it looks and because of some syntax I've never seen before that are hard to search on google (like "..."). The `luckySum(int... values)` method does it automatically take any parameters you input and store them in an array called `values`? And why static? – darkphoton Aug 22 '16 at 12:04
  • @darkphoton the [... are varargs](http://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java), basicly an `array` as parameter, but you can call the method as `luckyNumber(1,2,3,4,5,6,7,....., n);` with as many parametrs as an array can hold. Static means, you can call something without operating on an instance of an object, like calling a method in the main for example. [Check So question for a better explenation](http://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java). – SomeJavaGuy Aug 22 '16 at 12:19