Using basic Java programmingData will be read in from 5 dat
Using basic Java programming-Data will be read in from 5 .dat files. Please only use the Main method and no others. Also we were told we would need to use \"reader.hasNext()\" to read in the data.
Jarod D cs116 prog4 pdf x A a f O E D P E O M N SHI m EM ET O E D Elite Deal Club e Elementary Programm C Chegg le and Execute Write a program that reads a series of student names and exam scores from an input file, then calculates and prints the following: Each student\'s information The total number of students The maximum score The minimum score The average of all scores Notes Each input file contains different number of records nes). Each record consists of a student\'s last name and a single exam score. For this program you can assume that all data will be valid. There is no need to check for invalid exam scores. Use printf to format all exam scores to 2 decimal places. Use printf to display student information in columns with consistent width In addition to each student\'s name and exam score, print a student number corresponding to the order it was read from the data file (see Sample output for an example) Your Submit printout should use all the sample data in my examples below. You should have at least 5.dat files. You may have more than 5 (if you want to add your own test data), but not fewer than 5. other bookmarksSolution
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()){
String name = fileScan.next();
int score = fileScan.nextInt();
nameList.add(name);
list.add(score);
}
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();
}
double sum = 0;
for(int i=0; i<list.size(); i++){
sum = sum + Double.parseDouble(list.get(i)+\"\");
}
double average = (double)sum/list.size();
int maxScore = Integer.parseInt(list.get(0)+\"\");
for(int i=1; i<list.size(); i++){
if(maxScore < Integer.parseInt(list.get(i)+\"\")){
maxScore = Integer.parseInt(list.get(i)+\"\");
}
}
int minScore = Integer.parseInt(list.get(0)+\"\");
for(int i=1; i<list.size(); i++){
if(minScore > Integer.parseInt(list.get(i)+\"\")){
minScore = Integer.parseInt(list.get(i)+\"\");
}
}
System.out.println(\"The total number of students: \"+nameList.size());
System.out.println(\"The maximum score is \"+maxScore);
System.out.println(\"The minimum score is \"+minScore);
System.out.printf(\"THe average of all scores: %.2f\ \",average);
}
else{
System.out.println(\"File does nt exist\");
}
}
}
Output:
Enter an input file name :
D:\\\\scores.txt
Student Name Student Score
Suresh 55
Sekhar 77
Anshu 88
Revathi 99
The total number of students: 4
The maximum score is 99
The minimum score is 55
THe average of all scores: 79.75
scores.txt
Suresh 55
Sekhar 77
Anshu 88
Revathi 99

