Guest User

Untitled

a guest
Apr 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Assignment5{
  5. public static void main(String[] args)throws Exception{
  6. String data;
  7.  
  8. ArrayList<Double> store = new ArrayList<Double>();
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. System.out.println(" ");
  12. System.out.println("Please enter the name of a file with .txt at the end.");
  13. System.out.println(" ");
  14. data = scanner.nextLine();
  15.  
  16. File inputFile = new File(data);
  17. Scanner reader = new Scanner(inputFile);
  18.  
  19. while(reader.hasNext()){
  20. store.add(reader.nextDouble());
  21. }
  22.  
  23. calculateSum(store);
  24. store.clear();
  25.  
  26. }
  27.  
  28. private static void calculateSum(ArrayList<Double> ArrayList){
  29. double sum = 0;
  30. double avg = 0;
  31. double total = 0;
  32. double totalrainfall = 0;
  33.  
  34. Iterator<Double> iterator = ArrayList.iterator();
  35.  
  36. while(iterator.hasNext()){
  37. sum += iterator.next();
  38. }
  39.  
  40. total = ArrayList.size();
  41. avg = (sum / total);
  42.  
  43. System.out.println(" ");
  44. System.out.println("The total rainfall is " + totalrainfall);
  45.  
  46. }
  47.  
  48. import java.util.*;
  49. import java.io.*;
  50.  
  51. class Assignment5{
  52. public static void main(String[] args)throws Exception{
  53. String data;
  54.  
  55. ArrayList<Double> store = new ArrayList<Double>();
  56. Scanner scanner = new Scanner(System.in);
  57.  
  58. System.out.println(" ");
  59. System.out.println("Please enter the name of a file with .txt at the end.");
  60. System.out.println(" ");
  61. data = scanner.nextLine();
  62.  
  63. File inputFile = new File(data);
  64. Scanner reader = new Scanner(inputFile);
  65.  
  66. while(reader.hasNext()){
  67. store.add(reader.nextDouble());
  68. }
  69.  
  70. calculateSum(store);
  71. store.clear();
  72.  
  73. }
  74.  
  75. private static void calculateSum(ArrayList<Double> ArrayList){
  76. double sum = 0;
  77. double avg = 0;
  78. double total = 0;
  79. double totalrainfall = 0;
  80.  
  81. Iterator<Double> iterator = ArrayList.iterator();
  82.  
  83. while(iterator.hasNext()){
  84. sum += iterator.next();
  85. }
  86.  
  87. total = ArrayList.size();
  88. avg = (sum / total);
  89.  
  90. System.out.println(" ");
  91. System.out.println("The total rainfall is " + totalrainfall);
  92.  
  93. }
  94.  
  95. 80.0
  96. 70.0
  97. 75.0
  98. // ...
  99.  
  100. import java.util.;
  101. import java.io.;
  102.  
  103. class Assignment5
  104. {
  105.  
  106. public static void main(String[] args) throws Exception
  107. {
  108. String data;
  109. int[] store = new int[12];
  110. Scanner scanner = new Scanner(System.in);
  111.  
  112. System.out.println(" ");
  113. System.out.println("Please enter the name of a file with .txt at the end.");
  114. System.out.println(" ");
  115. data = scanner.nextLine();
  116.  
  117. File inputFile = new File(data);
  118. Scanner reader = new Scanner(inputFile);
  119.  
  120. while(reader.hasNext())
  121. {
  122. String currentLine = scanner.nextLine();
  123. String[] currentMonthDetails = currentLine.split(".");
  124. int currentMonth = Integer.parseInt(currentMonthDetails[0]);
  125. int currentRainfall = Integer.parseInt(currentMonthDetails[1]);
  126. store[currentMonth] = currentRainfall;
  127. }
  128.  
  129. calculateSum(store);
  130. store = null;
  131. }
  132.  
  133. private static void calculateSum(int[] aRainfallData)
  134. {
  135. int avgRainfall = 0;
  136. int numberOfMonths = 0;
  137. int totalRainfall = 0;
  138.  
  139. numberOfMonths = aRainfallData.length;
  140.  
  141. for (int index=0; index < numberOfMonths ; index++)
  142. {
  143. totalRainfall += aRainfallData[index];
  144. }
  145.  
  146. avgRainfall = (totalRainfall / numberOfMonths );
  147.  
  148. System.out.println(" ");
  149. System.out.println("The total rainfall is " + totalRainfall );
  150. }
  151. }
  152.  
  153. double sum = 0;
  154. boolean toggle = false;
  155. for(Object d: arrayList) {
  156. [...]
  157. if(toggle) {
  158. sum += (Double)d;
  159. toggle = !toggle;
  160. }
  161. }
Add Comment
Please, Sign In to add comment