2D Array Grades Example

Run Settings
LanguageJava
Language Version
Run Command
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 }
Student Zero 98 64 48 84 Student One 98 76 48 97 Student Two 91 73 78 93 Student Three 99 77 48 88
Editor Settings
Theme
Key bindings
Full width
Lines