Answer on Question #55470, Programming / Java
Solution
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Main class
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//one-dimensional array to store the student name
String studentNames[] = new String[5];
//(parallel) two-dimensional array to store the test score
int testScores[][] = new int[5][5];
//a parallel one-dimensional array to store grades
char storeGrades[] = new char[5];
//read data from file
int totalStudents = read(studentNames, testScores);
print(totalStudents, studentNames, testScores);
}
/**
* Read students from file students.txt
*/
}
private static int read(String studentNames[], int testScores[][]){
BufferedReader br = null;
int counter=0;
try {
String fileLine;
br = new BufferedReader(new FileReader("students.txt"));
while ((fileLine = br.readLine()) != null) {
//get name from file
studentNames[counter] = fileLine.split(" ")[0];
//get score 1 from file
testScores[counter][0] = Integer.parseInt(fileLine.split(" ")[1]);
//get score 2 from file
testScores[counter][1] = Integer.parseInt(fileLine.split(" ")[2]);
//get score 3 from file
testScores[counter][2] = Integer.parseInt(fileLine.split(" ")[3]);
//get score 4 from file
testScores[counter][3] = Integer.parseInt(fileLine.split(" ")[4]);
//get score 5 from file
testScores[counter][4] = Integer.parseInt(fileLine.split(" ")[5]);
counter++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return counter;
}
/**
* calculate the average test score
* @return
*/
private static double calculateAverageTestScore(int testScores[][], int studentIndex){
double sum=0;
for(int s=0;s<5;s++){
//calculate total sum of scores of selected student
sum+=testScores[studentIndex][s];
}
return sum/5;
}
/**
* getGrade
* @param averageTestScore
* @return
*/
private static char getGrade(double averageTestScore){
//85 - 100 A
if(averageTestScore>=85){
return 'A';
}
//75 - 84 B
if(averageTestScore>=75){
return 'B';
}
//65- 74 C
if(averageTestScore>=65){
return 'C';
}
//50 - 64 D
if(averageTestScore>=50){
return 'D';
}
//<50 F
return 'F';
}
/***
* calculate Class Average
* @param testScores
* @return
*/
private static double calculateClassAverage(int testScores[][]){
double sum=0;
for(int i=0;i<5;i++){
for(int s=0;s<5;s++){
//calculate total sum of all scores
sum+=testScores[i][s];
}
}
return sum/25;
}
/**
* method to output the result.
* @param totalStudents
* @param studentNames
* @param testScores
*/
private static void print(int totalStudents,String studentNames[],int testScores[][]){
//display information
for(int i=0;i<totalStudents;i++){
System.out.print(studentNames[i]+": ");
for(int s=0;s<5;s++){
System.out.print(testScores[i][s]+" ");
}
//calculate average Test Score
double averageTestScore=calculateAverageTestScore(testScores,i);
//display averageTestScore
System.out.println(" average test score: " + averageTestScore + " grade: " + getGrade(averageTestScore));
}
//display Class Average
System.out.println("\nClass Average: " + calculateClassAverage(testScores)+" = " + getGrade(calculateClassAverage(testScores)));
}
}
Comments
Yes, it can be skipped.
char storeGrades[]=new char[5]; what is the function of this line? are not in use
You need to change arrays capacity in main function from 5 to 10 and change maximal counter value in loops from 5 to 10 in functions, where arrays are processed.
what if i have 10 name instead ?
Dear vinod put the file "student.txt" to the same directory student.txt Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 23 30 69 87 Blair 23 45 96 38 59
i m gettting arrayindex bound exception
Dear Louis, file students.txt contains such fields: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 23 30 69 87 Blair 23 45 96 38 59
So what is the format of the students.txt?