So, I'm new to programming and I have this exercise where I have to read an int[][] array with the age and a handicap level of people trying to start a membership in a Club, with two categories, Senior and Open.
My job is to read the array for example [[45, 12],[55,21],[19, -2]] where the first int is the age and the second the handicap level. If the age is at least 55 and the handicap level is higher than 7 then the person gets a Senior membership if not he gets an Open one. My idea was to see the int[][] as a matrix and add both numbers (age and level) and if the number is higher than 62 I would classify it as a Senior otherwise as open.
My method looks like this:
public class montecarlo {
static String[] openOrSenior(int[][] a) {
int i, j, sum;
String[] abo = new String[a[0].length];
for (i = 0; i < a.length; i++)
for (j = 0; j < a[0].length; j++ ) {
sum = 0;
int x = a[i][j];
sum = sum + x;
if (sum > 62)
abo[i] = "Senior";
else
abo[i] = "Open"; //this would be line 12
}
return abo;
}
public static void main(String[] args) {
int [][] a = {{42, 12},{55, 21},{19,-2}};
String[] x = openOrSenior(a); //this would be line 20
Out.print(x); //here was to see what i'd get if i let it run
}
}
This is the error i get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at montecarlo.openOrSenior(montecarlo.java:12) at montecarlo.main(montecarlo.java:20)
I would really appreciate some help.
for (j = 0; j < a[i].length; j++ )String[] abo = new String[a.length];int[][]? Can you create a class, such asClubCandidateto hold the age and handicap? Then there would be aList<ClubCandiates> candidates. And a method such asgetMembershipType(). This would keep all the data together instead of having to manage separate arrays.