Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Numbers {
  5.  
  6.     public static void main (String [] args) throws FileNotFoundException {
  7.         Scanner input = openFile();
  8.  
  9.         double max = Double.MIN_VALUE;
  10.         double min = Double.MAX_VALUE;
  11.         double sum = 0;
  12.         double count = 0;
  13.        
  14.         while (input.hasNext()) {
  15.             if (input.hasNextDouble()) {
  16.                 count++;
  17.                 double num = input.nextDouble();
  18.                 if (num > max)
  19.                     max = num;
  20.                 if (num < min)
  21.                     min = num;
  22.                 sum += num;
  23.             } else {
  24.                 input.next();
  25.             }
  26.         }
  27.        
  28.  
  29.         System.out.println("Maximum: " + max);
  30.         System.out.println("Minimum: " + min);
  31.         System.out.println("Sum: " + sum);
  32.         System.out.println("Average: " + (sum/count));
  33.  
  34.     }
  35.  
  36.     public static Scanner openFile() throws FileNotFoundException {
  37.         System.out.print("Enter file name: ");
  38.         Scanner input = new Scanner(System.in);
  39.         String fileName = input.nextLine();
  40.         File inFile = new File(fileName);
  41.         while (!inFile.exists()) {
  42.            System.out.println(fileName + " not found.");
  43.            System.out.print("Enter file name: ");          
  44.            fileName = input.nextLine();
  45.            inFile = new File(fileName);
  46.     }
  47.         return new Scanner(inFile);
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement