Q:

How do I display the average of my array? 

I'm making a code that will take the inputs of a teacher so they can calculate the average grade for their class, with a few other things as well. 
Unfortunately, my code isn't working properly.  I am trying to get it to display the average grade but it keeps generating an error message.  
I need the code to take the class size first.  Then take the names of the students and their grades. 
Then It should calculate the average grade for the class.  This is what I have so far:
package program;

import java.util. Scanner;
public class FinalGrades{   
        public static void main(String[]args) {
            Scanner input = new Scanner(System.in);
            System.out.print("How many students are in your class? "); 
            int totalStudents = Integer.parseInt(input.nextLine());     
            String[] names = new String[totalStudents];
            double[] scores = new double[totalStudents];
            for(int i = 0; i < totalStudents; i++){
                System.out.print("Name: ");
                names[i] = input.next();
                System.out.print("Score: ");
                scores[i] = input.nextDouble();
            }
            double avg = 0;
            double sum = 0;
            for (int j = 0; j<= totalStudents; j++){
                sum += scores[j];
                avg = sum/totalStudents;
                System.out.println("The average score was: " + avg);    
            }

        }

}       

I would also like the code to display the highest grade with the students name next to it like: 
94.5, Jacob
     (but that isn't necessary right now). 

A:

Your last for is wrong, you don't calculate avg there, but after for is finished (after you have a sum of all scores).  Second, calculate sum as soon as their grades are taken.  In order to know student with max grade, as soon the input is given, check for max grade and remember if it is max.  Since there can be multiple such students with same best grade, save their position in array. 
 ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
 ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
 double maxGrade = 0;
 double minGrade = 101;
 double avg = 0;
 double sum = 0;
 for(int i = 0; i < totalStudents; i++){
      System.out.print("Name: ");
      names[i] = input.next();
      System.out.print("Score: ");
      scores[i] = input.nextDouble();
      sum += scores[i];
      System.out.println("Running avg: " + (sum / (i + 1)); // for extra points
      if (scores[i] > maxGrade) {
          bestStudentPosition.clear(); // delete all previous students from list
          maxGrade = scores[i];
          bestStudentPosition.add(new Integer(i));
      } 
      else if (scores[i] == maxGrade) {  // if its same as some student(s) before, add this one to list too
           bestStudentPosition.add(new Integer(i)); 
      } else if (scores[i] < minGrade) {
           worstStudentPosition.clear();
           minGrade = scores[i];
           worstStudentPosition.add(new Integer(i));
      } else if (scores[i] == minGrade) {
           minGrade= scores[i];
           worstStudentPosition.add(new Integer(i));
      }

 }

 avg = sum/totalStudents;   // final avg
 System.out.println("The average score was: " + avg);  
 System.out.println("Best student(s):");
 for (Integer position : bestStudentPosition) { // print all best students
      System.out.println(maxGrade + ", " + names[position]);
 }
 System.out.println("Worst student(s):");
 for (Integer position : worstStudentPosition) { // print all worst students
      System.out.println(minGrade + ", " + names[position]);
 }

