Guest User

Untitled

a guest
Sep 21st, 2019
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.InputMismatchException;
  5. import java.util.Scanner;
  6.  
  7. class AssignmentOne {
  8. public static void main(String[] args) throws FileNotFoundException, InputMismatchException {
  9.  
  10. File file = new File("Numbers.txt");
  11.  
  12. try {
  13. // Initialize Scanner with file
  14. Scanner in = new Scanner(file);
  15.  
  16. double[] arr = new double[10]; // Initialize array
  17. Arrays.fill(arr, 0.0); // Fills all the array positions with 0.0
  18.  
  19. // Maximum doubles is 10, but we are not sure if the text
  20. // file has 10 numbers so we use hasNextDouble
  21. double sum = 0;
  22. while (in.hasNext()) {
  23. try {
  24. sum += in.nextDouble();
  25. } catch (InputMismatchException e) {
  26. System.out.println("Found a non-numeric value in the file!");
  27. in.close();
  28. return;
  29. }
  30. }
  31. System.out.println(sum);
  32. in.close();
  33. } catch (FileNotFoundException e) {
  34. System.out.println("Cannot find the file Numbers.txt");
  35. }
  36. }
  37. }
RAW Paste Data