Advertisement
Guest User

Rainfall TXT average help

a guest
Apr 24th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. static void rainfall() {
  2. try {
  3. System.out.print("Enter the file name with extension : ");
  4. // C:\Users\Rach2o4u\workspace\Project 4\src\rainfall.txt
  5. Scanner input = new Scanner(System.in);
  6.  
  7. File file = new File(input.nextLine());
  8. input.close();
  9.  
  10. input = new Scanner(file);
  11.  
  12. do {
  13. String line2 = input.nextLine();
  14.  
  15. double value1 = Double.parseDouble(line2);
  16.  
  17. if (value1 == -999)
  18. break;
  19. String line = null;
  20. double sum = 0;
  21. double count = 0;
  22. while ((line = input.nextLine()) != null) {
  23. String[] vals = line.split(" ");
  24. for(int i = 0; i < vals.length; i++) {
  25. sum += Double.valueOf(vals[i].trim());
  26. count++;
  27. }
  28. }
  29. System.out.println("the average is = " + ((double) sum / count));
  30.  
  31.  
  32.  
  33. System.out.println(value1);
  34. } while (input.hasNextLine());
  35. input.close();
  36.  
  37. } catch (Exception ex) {
  38. ex.printStackTrace();
  39. }
  40. }
  41. private static int countnums(String file) {
  42. int count = 0;
  43. if (line.charAt(0) != ' ') {
  44. count++;
  45. }
  46. for (int i = 0; i < line.length(); i++) {
  47. if ((line.charAt(i) == ' ')) {
  48. count++;
  49. }
  50. }
  51. return count;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement