I'm trying to find the average of all the values that come out of this for loop. I've created a variable that adds the values together once they're created (k
), and I need to be able to work on the final k
. The problem is that I can't access the k
unless I'm in the if
statement, which means that I can't print or work on the final version. How would I access it so that I could use it outside the for loops and if statements?
public static void main(String[] args) {
String[][] act = Activities.act;
String[] nd = Youngadult.normalday;
int nl=nd.length;
int al=act.length;
int b = 0;
int k = 0;
for(int i = 0; i <= nl; i++){
for(int j=0; j <= al; j++) {
if(act[i][0] == nd[j]) {
b = j;
int mt = Integer.parseInt(act[b][2]);
int l = Integer.parseInt(act[b][3]);
int p = Integer.parseInt(act[b][4]);
int r = calc(mt, sl, p, l, c, a, n, o, e);
k +=r;
System.out.println(k);
break;
}
}
} System.out.println(k);
}
These are the arrays I reference in the code:
static String[][] act = {{"Dog Walking", "Home Chore", "1", "2", "0", "true"},
{"Hanging Out With Friends", "Socialisation", "5", "3", "5", "false"},
{"Homework", "Education", "1", "2", "0", "true"},
{"Watching a Movie", "Entertainment", "4", "2", "0", "true"},
{"Painting", "Entertainment", "4", "3", "0", "true"}};
}
String[] normalday= new String[] {"Dog Walking",
"Hanging Out With Friends", "Homework",
"Watching a Movie", "Painting"};
and this is what is outputted:
-10
35
25
35
35
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at youServe.TestingForLoop.collate(TestingForLoop.java:112)
at youServe.TestingForLoop.main(TestingForLoop.java:141)