2D Array Grades Example
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
import java.io.*; //import everything from java.io
import java.util.*; //import everything from java.util
class Main {
public static void main(String[] args) {
String [] students = new String[10];
int [][] grades = new int [10][10];
try
{
File f = new File("data.txt");
Scanner s = new Scanner(f);
while(s.hasNextLine())
{
//nested for loop to sift the information
for(int n=0;n<4;n++)
{
students[n]=s.nextLine();
for(int g=0; g<4; g++)
{
grades[n][g] = Integer.parseInt(s.nextLine());
}
}
}
s.close();
}
catch (FileNotFoundException f){
System.out.println("No File Found!");
}
catch(NoSuchElementException n){
System.out.println("No Elements Found!");
}
//required variable
listGrades(students,grades); //run custom list grades function
studentAverage(students,grades); //run custom student average function
// //run the custom add student data function
// //run the custom class average function
} //processes the information from the file
public static void listGrades(String[]nameList, int[][]gradeList){
int studentChoice = 3;
int startingPoint = studentChoice-1;
System.out.print(nameList[studentChoice]+": ");
for(int i=startingPoint;i<studentChoice; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
System.out.print(gradeList[studentChoice][gradesInRow]+" ");
}
}
} //list all of the grades for a specifiuc student
public static void studentAverage(String[]nameList, int[][]gradeList){
int studentChoice = 3;
int startingPoint = studentChoice-1;
double avg = 0;
for(int i=startingPoint;i<studentChoice; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
avg+=gradeList[studentChoice][gradesInRow];
}
}
avg = avg/4;
System.out.println("\nThe Average:"+ avg);
} //calculate the average
}
Java
INFO