Advertisement
vencinachev

Students Main

Nov 16th, 2020
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class Program {
  8.  
  9.     public static void main(String[] args) {
  10.            
  11.         try {
  12.             File inputFile = new File("students.txt");
  13.             Scanner filescan = new Scanner(inputFile);
  14.            
  15.             File outputFile = new File("studentsresult.txt");
  16.             PrintWriter pw = new PrintWriter(outputFile);
  17.            
  18.             ArrayList<Student> stds = new ArrayList<Student>();
  19.            
  20.             while (filescan.hasNextLine()) {
  21.                 String name = filescan.nextLine();
  22.                 int number = Integer.parseInt(filescan.nextLine());
  23.                 double grade = Double.parseDouble(filescan.nextLine());
  24.                 stds.add(new Student(name, number, grade));
  25.             }
  26.            
  27.             int minGradeIndex = 0;
  28.             int maxGradeIndex = 0;
  29.             double sumGrades = 0;
  30.            
  31.             for (int i = 0; i < stds.size(); i++) {
  32.                 sumGrades += stds.get(i).getGrade();
  33.                 if (stds.get(i).getGrade() < stds.get(minGradeIndex).getGrade()) {
  34.                     minGradeIndex = i;
  35.                 }
  36.                 if (stds.get(i).getGrade() > stds.get(maxGradeIndex).getGrade()) {
  37.                     maxGradeIndex = i;
  38.                 }
  39.             }
  40.            
  41.             double avrg = sumGrades / stds.size();
  42.            
  43.             System.out.println("Average grades: " + avrg);
  44.             System.out.println("Max grade student: " + stds.get(maxGradeIndex));
  45.             System.out.println("Min grade student: " + stds.get(minGradeIndex));
  46.            
  47.             pw.println("Average grades: " + avrg);
  48.             pw.println("Max grade student: " + stds.get(maxGradeIndex));
  49.             pw.println("Min grade student: " + stds.get(minGradeIndex));
  50.            
  51.             filescan.close();
  52.             pw.close();
  53.         } catch (FileNotFoundException e) {
  54.             System.out.println("Sorry! File is not found!");
  55.         }
  56.     }
  57.  
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement