/home/caleb/ASDV-Java/Assignments/MP3_CalebFontenot/src/mp3_calebfontenot/Stats.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package mp3_calebfontenot;

import java.util.Scanner;

/**
 *
 * @author caleb
 */
public class Stats {

    public static void main(String[] args)
    {
        // Create scanner
        Scanner input = new Scanner(System.in);
        // Define variables
        double mean = 0; //It's twice as mean
        double inputSum = 0, numberOfTimesRun = 0, inputPow = 0, deviation = 0;
        String userInput = "";

        // Prompt for input
        do {
            System.out.println("Press Y/y to enter a series of numbers or Q/q to quit.");
            System.out.println("Current sum: " + inputSum);
            userInput = input.next();
                if (userInput.toLowerCase().equals("y")) {
                    try {
                        do {
                            System.out.print("Please enter a series of numbers; Type '-1' to quit (no quotes, you doofus!): ");
                            userInput = input.next();
                            if (Double.parseDouble(userInput) != -1) {
                                inputSum += Double.parseDouble(userInput);
                                inputPow += Math.pow(Double.parseDouble(userInput), 2);
                                numberOfTimesRun++; // counter to store number of times we've added to input sum
                            }
                            if (numberOfTimesRun == 0) {
                                System.out.println("No numbers entered!");
                            }
                            System.out.println("Current sum: " + inputSum);
                            System.out.println("Input sum^2: " + inputPow);
                        } while (Double.parseDouble(userInput) != -1);
                    } catch (Exception e) {
                        System.out.println("Invalid input!");
                    }
                
            } else {
                System.out.println("Invalid input!");
            }
                // Calculate mean
                mean = inputSum / numberOfTimesRun;
                // Calculate deviation
                if (numberOfTimesRun == 1) { // if the loop has only run once,  don't bother calculating it, because the deviation is zero.
                    deviation = 0;
                } else {
                    deviation = Math.sqrt((inputPow - (Math.pow(inputSum, 2) / numberOfTimesRun)) / (numberOfTimesRun - 1));
                }
                System.out.println();
                System.out.println("The mean is: " + mean);
                System.out.println("The deviation is: " + deviation);

            }
            while (!(userInput.toLowerCase().equals("q")));
        }
    }