import java.io.File; //import file library
import java.io.FileNotFoundException; //import exception library
import java.util.Scanner; //import the scanner libary
class Main {
public static void main(String[] args) {
//create empty arrays to hold info from the file
String [] studentNames = new String [10];
int [] grade = new int[10];
//create a counter for the array position
int arrayPositionCounter = 0;
//-------------------begin gathering and processing information--------------
try //try everything inside the brackets..
{
//make a new scanner and point to file location
File f = new File("studentData.txt");
Scanner textFileScanner = new Scanner(f);
//loop through entire file
while (textFileScanner.hasNextLine())
{
studentNames[arrayPositionCounter]=textFileScanner.nextLine(); //store the name
grade[arrayPositionCounter]=Integer.parseInt(textFileScanner.nextLine()); //store the grade
//output the information
System.out.print("Student Name: " + studentNames[arrayPositionCounter]);
System.out.print("\t Grade: " + grade[arrayPositionCounter] + "\n");
//add to the counter
arrayPositionCounter +=1;
}
//close the file scanner when we're done
textFileScanner.close();
}
catch (FileNotFoundException e) //catch the error
{
System.out.println("OH NO!!!! NO FILE EXISTS!!!");
e.printStackTrace(); //display the specific error
}
//-------------------end of information processing--------------
calculateAverage(grade); //determine the average
}
public static void calculateAverage(int[] arrayOfGrades)
{
int total=0;
for(int i=0; i<arrayOfGrades.length; i++){
total+=arrayOfGrades[i];
}
double avg = (total/arrayOfGrades.length);
System.out.println("Average is: " + avg);
}
}
Michael
78
Chris
84
Jessica
30
Matthew
98
Ashley
56
Jennifer
64
Joshua
77
Amanda
95
Daniel
99
David
65