Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1.  
  2. package weightedavgdataanalyzer;
  3.  
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8. import java.util.NoSuchElementException;
  9. import java.util.*;
  10.  
  11. public class WeightedAvgDataAnalyzer {
  12.  
  13.     public static void main(String[] args) throws IOException{
  14.         Scanner in = new Scanner(System.in);
  15.  
  16.  
  17.         boolean done = false;
  18.         while (!done){
  19.             try{
  20.                 System.out.print("Please enter the file name: ");
  21.                 String filename = in.next();
  22.                 System.out.println();
  23.  
  24.                 ArrayList<Double> data = readFile(filename);
  25.  
  26.                 System.out.println("Weighted average: " + calcWeightedAverage(data));
  27.  
  28.                 done = true;
  29.             }
  30.  
  31.             catch (FileNotFoundException exception){
  32.                 System.out.println("File not found.");
  33.                 return;
  34.             }
  35.  
  36.             catch (NoSuchElementException exception){
  37.                 System.out.println("File contents invalid.");
  38.             }
  39.  
  40.             catch (IOException exception){
  41.                 exception.printStackTrace();
  42.             }
  43.         }
  44.     }
  45.  
  46.     public static ArrayList<Double> readFile(String filename) throws IOException{
  47.         File inFile = new File(filename);
  48.         try(Scanner in = new Scanner(inFile)) {
  49.             return readData(in);
  50.         }
  51.     }
  52.  
  53.     public static ArrayList<Double> readData(Scanner in) throws IOException{
  54.         String[] nameOfStringArray;
  55.         ArrayList<Double> data = new ArrayList<>();
  56.          
  57.         // grab all the data from the file
  58.         try {
  59.             if (in.hasNextLine()) {
  60.                 nameOfStringArray = in.nextLine().split(" ");
  61.                 for (String stringElement : nameOfStringArray) {
  62.                     data.add(Double.parseDouble(stringElement));
  63.                 }
  64.             }
  65.         }
  66.        
  67.         catch (Exception e) {}
  68.  
  69.         return data;
  70.     }
  71.    
  72.     private static double getWeight(ArrayList<Double> data) {
  73.         return data.get(0);
  74.     }
  75.  
  76.     private static int getDrop(ArrayList<Double> data) {
  77.         return data.get(1).intValue();
  78.     }
  79.  
  80.     public static double calcWeightedAverage(ArrayList<Double> data) {
  81.         double weight = getWeight(data);
  82.         int drops = getDrop(data);
  83.         for (int i = 0; i < drops; i++){
  84.             int minIntIndex = 2;
  85.             for (int x = 3; x < data.size(); x++){
  86.                 if (data.get(x) < data.get(minIntIndex)) {
  87.                     minIntIndex = x;
  88.                 }
  89.  
  90.             }
  91.              data.remove(minIntIndex);  
  92.         }
  93.         data.remove(0);
  94.         data.remove(0);
  95.         ArrayList<Double> newArray = new ArrayList<>(data);
  96.        
  97.         for(int i = 0; i < newArray.size(); i++) {
  98.             newArray.set(i, newArray.get(i) * weight);
  99.         }
  100.        
  101.         double sum = 0.0;
  102.         for(int i = 0; i < newArray.size(); i++){
  103.             sum += newArray.get(i);
  104.         }
  105.        
  106.         double average = sum / newArray.size();
  107.         return average;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement