Advertisement
FidoDidoo100

Exercise1

Nov 9th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package Gotseva;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Exercise1 {
  9.  
  10.     public static void main(String[] args) {
  11.        
  12.         double sum[] = new double[1000];
  13.         numbersReader("numbers.txt", sum);
  14.         printNumbers(sum);
  15.         double sumTotal = sumNumbers(sum);
  16.         System.out.println(sumTotal);
  17.        
  18.     }
  19.    
  20.     public static void numbersReader(String fname, double[] days){
  21.         try {
  22.             Scanner sc = new Scanner (new BufferedReader(new FileReader(fname)));
  23.             for (int i=0; i<1000; i++){
  24.                 if (!sc.hasNext()){
  25.                     throw new IOException("No data!");
  26.                 }
  27.                 days[i] = sc.nextInt();
  28.             }
  29.         }
  30.         catch (FileNotFoundException ex){
  31.         } catch (IOException ex){
  32.         }
  33.     }
  34.    
  35.     public static double sumNumbers (double[] days){
  36.        
  37.         double sumTotal = 0;
  38.         for (int i=0; i<1000; i++){
  39.             sumTotal += days[i];
  40.         }
  41.         return sumTotal;
  42.     }
  43.    
  44.     public static void printNumbers(double[] days){
  45.         for (int i=0; i<1000; i++){
  46.             System.out.print(days[i] + " ");
  47.         }
  48.         System.out.println();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement