Rilett Class Average

Run Settings
LanguageJava
Language Version
Run Command
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; class Main { public static void main(String[] args) { //create empty arrays for data from the file String [] studentNames = new String[10]; int [] grade = new int [10]; //create a counter for the array position int c = 0; // try and catch scenarios try { File f = new File("studentData.txt"); Scanner s = new Scanner (f); System.out.println("Mr. Rilett's Class Report"); System.out.println("-------------------------"); //loop through our file while (s.hasNextLine()) { studentNames[c] = s.nextLine(); grade[c] = Integer.parseInt(s.nextLine()); //output information to double check its correct System.out.print("Student Name: " + studentNames[c]); System.out.println("\t Grade: " + grade[c]); c+=1; //move to next position } s.close(); } catch (FileNotFoundException e) { System.out.println("No File Exists."); //message to user e.printStackTrace(); //specific error } //calculate the average calculateAverage(grade); } public static void calculateAverage(int [] theGradeArrayFromTheMainMethod) { int runningTotal=0; //add each grade to this running total for (int i=0;i<theGradeArrayFromTheMainMethod.length;i++){ runningTotal+=theGradeArrayFromTheMainMethod[i]; } double classAverage = (runningTotal/theGradeArrayFromTheMainMethod.length); System.out.println("Class Average is: " + classAverage); } }
Michael 78 Chris 84 Jessica 30 Matthew 98 Ashley 56 Jennifer 64 Joshua 77 Amanda 95 Daniel 99 David 65
Editor Settings
Theme
Key bindings
Full width
Lines