0

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)
Kay
  • 27
  • 5
  • 4
    Your assertion that you can't access `k` outside of the if-statement is completely wrong. You should be able to access it anywhere in the function. – M-Chen-3 Feb 22 '21 at 18:13
  • the code seems fine to me! the `k` variable should be accessible from outside of the loop! – XO56 Feb 22 '21 at 18:14
  • @XO56 do you have any idea why asking it to do System.out.println(k); after the first for loop's last curly bracket returns nothing? – Kay Feb 22 '21 at 18:22
  • @Kay if you have a problem with your code, please [edit] your question to include code that reproduces the problem, not something that gets us half-way there. If you have a problem with a `System.out.println(k);` that's not in your code, please show that as well. – Federico klez Culloca Feb 22 '21 at 18:25
  • 3
    Printing `k` after the loop should at least print `0`. If that's not happening, there must be some error (e.g. parse exception) displayed. – John Bayko Feb 22 '21 at 18:29
  • 1
    @Kay After your edit, yes, that should print at least `0`, if not another number. But it's impossible for us to tell the exact result because we don't know what `Activities.act` and `Youngadult.normalday` are. So, as John said, either this prints a number or it's throwing an exception before getting there. – Federico klez Culloca Feb 22 '21 at 18:35
  • 3
    You're definitely running into an `ArrayIndexOutOfBoundsException`, because the conditions should be `i < nl` and `j < al` not `i <=nl` resp. `j <=al`, also you're accessing the wrong array with the index: `act[i][0]` should be `act[j][0]` and `nd[j]` should be `nd[i]` – Lino Feb 22 '21 at 18:36
  • as @Lino mentioned, there is a runtime error in the for loop. that's why the compiler never gets to the `System.out.println(k);`. – XO56 Feb 22 '21 at 18:41
  • @Lino that's exactly what happened! Everything seems to be doing what I need it to now. – Kay Feb 22 '21 at 18:44
  • @XO56 thank you, you're right haha. i've got it fixed now – Kay Feb 22 '21 at 18:46
  • you're welcome. :) – XO56 Feb 22 '21 at 18:47
  • @Kay for the future, it is very advisable to **always** add any errors you're getting to your question. Without them a lot of guesswork is required. But when you'd have told us that you're getting an exception in your loop, you'd have gotten your answer almost immediately – Lino Feb 22 '21 at 18:48
  • @Lino i'll remember that for next time! thank you! – Kay Feb 22 '21 at 18:51

0 Answers0