1

I'm struggling to learn arrays in Java. I have some code which includes an array of Integer for attendance over 10 weeks with a class size of 15. The task is to output the percentage attendance for each week. I've managed to create a for loop to access each value in the array and I have done what I think is correct to cast these to double so that I can work out the percent of each. I encounter issues when trying to output results. Firstly when trying to println the error is that i has not been initialised. I have i in my for loop and if i take it out of the for loop i get output of 8.0 which is not what I want. Secondly, I would like all results to be numbered 1 to 10 but again the +i+ in println is not working for me and I don't know why. Could someone tell me what I have done wrong with i? Thanks

public class AttendanceTest {

public static void main(String[] args) {
    final Integer CLASS_SIZE = 15;
    Integer[] attendances = {10, 14, 12, 9, 11, 15, 12, 13, 8, 14};
   double attendance=0;

   {for(int i=0;i<attendances[i];i++)
         attendance=(double) (attendances[i] / 15)*100;

    System.out.println(+i+attendance );
    }
}

}

1
  • 1
    The way you currently have it, the calculation is the only thing that's controlled by the for loop. The println is not part of the loop. Commented Feb 1, 2018 at 17:31

3 Answers 3

1

The curly brace must come after the for loop declaration. Additionally, you have to cast attendances[i] to double inside the parentheses, not outside. Thirdly, your solution is adding i to the percentage. You should add a space character in between so it doesn't add the two numbers. Fourthly, the stopping condition for the for loop should be when i is equal to the number of elements in attendances.

public static void main(String[] args) {
    final Integer CLASS_SIZE = 15;
    Integer[] attendances = {10, 14, 12, 9, 11, 15, 12, 13, 8, 14};
    double attendance;
    for (int i = 0; i < attendances.length; i++) {
        attendance = ((double) attendances[i] / CLASS_SIZE) * 100;
        System.out.println(i + " " + attendance);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are casting the result of the computation, but by that point it is too late. You need to cast attendances[i] as a double, so that integer division won't be used.

Comments

0

As you just need to output the attendance of each variable, you should put the print inside of the for-loop.

public static void main(String[] args) {
    final Integer CLASS_SIZE = 15;
    Integer[] attendances = {10, 14, 12, 9, 11, 15, 12, 13, 8, 14};

   for(int i = 0; i < attendances.length; i++){
         System.out.println(((double) attendances[i] / 15) * 100 + "% attendance" );
   }
}

A few things to note with your code:

  1. To get the length of an array, you need to use array.length.
  2. The casting of an double variable should be in front of an Integer-variable. This is because when you divide to integers, you will get an integer back. If you divide by a double and a integer, you will get an double back.
  3. I would rather divide by CLASS_SIZE instead of hard coding in 15 in your code.
  4. The curly braces on your for-loop is supposed to be after it, and encapsle whats in it. Like:

    for(int i = 0; i < 10; i++){ //What happens when it loops should go in here System.out.println("i is now " + i);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.