Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Numbers {
- public static void main (String [] args) throws FileNotFoundException {
- Scanner input = openFile();
- double max = Double.MIN_VALUE;
- double min = Double.MAX_VALUE;
- double sum = 0;
- double count = 0;
- while (input.hasNext()) {
- if (input.hasNextDouble()) {
- count++;
- double num = input.nextDouble();
- if (num > max)
- max = num;
- if (num < min)
- min = num;
- sum += num;
- } else {
- input.next();
- }
- }
- System.out.println("Maximum: " + max);
- System.out.println("Minimum: " + min);
- System.out.println("Sum: " + sum);
- System.out.println("Average: " + (sum/count));
- }
- public static Scanner openFile() throws FileNotFoundException {
- System.out.print("Enter file name: ");
- Scanner input = new Scanner(System.in);
- String fileName = input.nextLine();
- File inFile = new File(fileName);
- while (!inFile.exists()) {
- System.out.println(fileName + " not found.");
- System.out.print("Enter file name: ");
- fileName = input.nextLine();
- inFile = new File(fileName);
- }
- return new Scanner(inFile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement