Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.BufferOverflowException;
- import java.util.HashMap;
- import java.util.List;
- public class HW01_4 {
- public static void addStudents(File file){
- PrintWriter pw = null;
- BufferedReader bf = null;
- try {
- bf = new BufferedReader(new InputStreamReader(System.in));
- pw = new PrintWriter(new FileWriter(file));
- String line = null;
- System.out.println("Внесете Индекс, и оцена по предметите, разделени со запирка(,).");
- System.out.println("Студент | КРС | НРС | АОК");
- while ((line = bf.readLine()) != null){
- pw.println(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- pw.flush();
- pw.close();
- try {
- bf.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void writeAvg(File file, String path){
- double KPC = 0;
- double HPC = 0;
- double AOK = 0;
- int brojac = 0;
- double avg = 0;
- String line = null;
- BufferedReader bf = null;
- PrintWriter pw = null;
- File newFile = new File(path + "\\rezultati.tsv");
- try {
- if (!newFile.exists()){
- newFile.createNewFile();
- }
- bf = new BufferedReader(new FileReader(file));
- pw = new PrintWriter(new FileWriter(newFile));
- while ((line = bf.readLine()) != null){
- if (line.length() != 0) {
- String[] niza = line.split(",");
- for (int i = 1; i < niza.length; i++) {
- avg += Double.parseDouble(niza[i]);
- }
- pw.println(String.format("%s : %.2f", niza[0], (double) (avg / 3)));
- KPC += Double.parseDouble(niza[1]);
- HPC += Double.parseDouble(niza[2]);
- AOK += Double.parseDouble(niza[3]);
- brojac++;
- avg = 0.0;
- }
- }
- pw.println(String.format("KPC: %.2f", (double)(KPC/brojac)));
- pw.println(String.format("HPC: %.2f", (double)(HPC/brojac)));
- pw.println(String.format("AOK: %.2f", (double)(AOK/brojac)));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- pw.flush();
- pw.close();
- try {
- bf.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- String path = null;
- System.out.println("Vnesete pateka:");
- File rezultatiCsv = null;
- PrintWriter pw = null;
- try {
- path = bf.readLine();
- rezultatiCsv = new File(path + "rezultati.csv");
- if (!rezultatiCsv.exists()){
- rezultatiCsv.createNewFile();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- try {
- pw = new PrintWriter(new FileWriter(rezultatiCsv));
- pw.println("Студент,КРС,НРС,АОК");
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- pw.flush();
- pw.close();
- }
- addStudents(rezultatiCsv);
- writeAvg(rezultatiCsv, path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement