Write a program that reads a series of student names and exa
Solution
StudentScore.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentScore {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter an input file name : \");
String fileName = scan.next();
File file = new File(fileName);
if(file.exists()){
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<String> nameList = new ArrayList<String>();
Scanner fileScan = new Scanner(file);
while(fileScan.hasNextLine()){
nameList.add(fileScan.next());
list.add(fileScan.nextInt());
}
String heading1 = \"Student Name\";
String heading2 = \"Student Score\";
System.out.printf( \"%-15s %-15s\", heading1, heading2);
System.out.println();
for(int i=0; i<nameList.size(); i++){
System.out.printf( \"%-15s %-15s\", nameList.get(i), list.get(i));
System.out.println();
}
System.out.println(\"The total number of studnets: \"+nameList.size());
System.out.println(\"The maximum score is \"+maxScores(list));
System.out.println(\"The minimum score is \"+minScores(list));
System.out.printf(\"THe average of all scores: %.2f\ \",averageScores(list));
}
else{
System.out.println(\"File does nt exist\");
}
}
public static double averageScores(ArrayList scoreList){
double sum = 0;
for(int i=0; i<scoreList.size(); i++){
sum = sum + Double.parseDouble(scoreList.get(i)+\"\");
}
return (double)sum/scoreList.size();
}
public static int maxScores(ArrayList scoreList){
int maxScore = Integer.parseInt(scoreList.get(0)+\"\");
for(int i=1; i<scoreList.size(); i++){
if(maxScore < Integer.parseInt(scoreList.get(i)+\"\")){
maxScore = Integer.parseInt(scoreList.get(i)+\"\");
}
}
return maxScore;
}
public static int minScores(ArrayList scoreList){
int minScore = Integer.parseInt(scoreList.get(0)+\"\");
for(int i=1; i<scoreList.size(); i++){
if(minScore > Integer.parseInt(scoreList.get(i)+\"\")){
minScore = Integer.parseInt(scoreList.get(i)+\"\");
}
}
return minScore;
}
}
Output:
Enter an input file name :
D:\\\\scores.txt
Student Name Student Score
Suresh 45
Sekhar 55
Anshu 77
Revathi 99
The total number of studnets: 4
The maximum score is 99
The minimum score is 45
THe average of all scores: 69.00
scores.txt
Suresh 45
Sekhar 55
Anshu 77
Revathi 99

