Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Program {
- public static void main(String[] args) {
- try {
- File inputFile = new File("students.txt");
- Scanner filescan = new Scanner(inputFile);
- File outputFile = new File("studentsresult.txt");
- PrintWriter pw = new PrintWriter(outputFile);
- ArrayList<Student> stds = new ArrayList<Student>();
- while (filescan.hasNextLine()) {
- String name = filescan.nextLine();
- int number = Integer.parseInt(filescan.nextLine());
- double grade = Double.parseDouble(filescan.nextLine());
- stds.add(new Student(name, number, grade));
- }
- int minGradeIndex = 0;
- int maxGradeIndex = 0;
- double sumGrades = 0;
- for (int i = 0; i < stds.size(); i++) {
- sumGrades += stds.get(i).getGrade();
- if (stds.get(i).getGrade() < stds.get(minGradeIndex).getGrade()) {
- minGradeIndex = i;
- }
- if (stds.get(i).getGrade() > stds.get(maxGradeIndex).getGrade()) {
- maxGradeIndex = i;
- }
- }
- double avrg = sumGrades / stds.size();
- System.out.println("Average grades: " + avrg);
- System.out.println("Max grade student: " + stds.get(maxGradeIndex));
- System.out.println("Min grade student: " + stds.get(minGradeIndex));
- pw.println("Average grades: " + avrg);
- pw.println("Max grade student: " + stds.get(maxGradeIndex));
- pw.println("Min grade student: " + stds.get(minGradeIndex));
- filescan.close();
- pw.close();
- } catch (FileNotFoundException e) {
- System.out.println("Sorry! File is not found!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement