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));
}
}