Advertisement
Guest User

Untitled

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