This program takes a file from a user, "example.txt". The file contains only positive integers, and the first interger in the file is stating how many intergers come after that integer (Ex. 3, 45, 98, 102) It is safe to assume everything in the users file is properly formatted and they put .txt at the end. The program is pretty simple for the most part, it was mainly to practice Array's, but I'm curious if I can cut this down a bit, or if I over-complicated anything. This code works by the way.
import java.util.*;
import java.io.*;
import java.lang.*;
public class TestScoresAndSummaryStatistics
{
public static void main(String[] args) throws IOException
{
int i = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file");
String fileName = keyboard.nextLine();
File inputFile = new File(fileName);
Scanner data = new Scanner(inputFile);
//declaring that whatever the first number in the document is is how many indices the document has
int scores[] = new int[data.nextInt()];
while (data.hasNext())
{
scores[i] = data.nextInt();
i++;
}
Arrays.sort(scores);
//Highest and Lowest score
System.out.println("Lowest score: " + scores[0]);
System.out.println("Highest score: " + scores[(scores.length - 1)]);
//Calculating Mean
double gradesTotal = 0;
for (i=0; i<scores.length; i++)
{
gradesTotal = gradesTotal + scores[i];
}
double mean = gradesTotal/scores.length;
System.out.println("Mean score: " + mean);
//Calculating Standard Deviation
double variance = 0;
for (i=0; i<scores.length; i++)
{
variance = variance + (Math.pow((scores[i] - mean), 2));
}
variance = variance / scores.length;
double standardDeviation = Math.sqrt(variance);
System.out.println("Standard Deviation: " + standardDeviation);
//Finding Median score
double median;
if (scores.length % 2 == 0)
median = ((scores[(scores.length/2) - 1]) + scores[(scores.length/2)]) / 2;
else
median = scores[(scores.length/2)];
System.out.println("Median score: " + median);
//Finding how many number of A's, B's, etc
int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
int gradeF = 0;
for (i=0; i<scores.length; i++)
{
if (scores[i] >= 90)
{
gradeA++;
}
else if (scores[i] <= 89 && scores[i] >=80)
{
gradeB++;
}
else if (scores[i] <= 79 && scores[i] >=70)
{
gradeC++;
}
else if (scores[i] <= 69 && scores[i] >=60)
{
gradeD++;
}
else
{
gradeF++;
}
}
System.out.println("You have recieved " + gradeA + " A's, " + gradeB + " B's, " + gradeC + " C's, " + gradeD +
" D's, and " + gradeF + " F's");
}
}