Skip to content

Instantly share code, notes, and snippets.

@ParadauxIO
Created October 19, 2020 12:42
Show Gist options
  • Save ParadauxIO/aa3ece1abd7c1551b19d581fac5478f2 to your computer and use it in GitHub Desktop.
Save ParadauxIO/aa3ece1abd7c1551b19d581fac5478f2 to your computer and use it in GitHub Desktop.
Calculate the standard deviation of space-seperated terms, supplied via STDIN
import java.util.logging.Logger;
public class StandardDeviation {
public static void main(String[] args) {
Logger logger = Logger.getLogger("Standard Deviation");
double populationMean = 0;
double stdDeviationNumerator = 0;
double populationStdDeviation;
// No point calculating if there's only one value, and it's impossible to if no input is provided
if (args.length <= 1) {
logger.severe("Not enough values supplied.");
return;
}
populationMean = populationMean / args.length;
for (String arg : args) { stdDeviationNumerator += Math.pow( Double.parseDouble(arg) - populationMean, 2 ); }
populationStdDeviation = Math.sqrt(stdDeviationNumerator / args.length);
logger.info("Supplied values: [ " + String.join(", ", args) + " ]");
logger.info("Population Mean: " + populationMean);
logger.info("Standard Deviation: " + populationStdDeviation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment