Question
Here is my code,I don't know why the output is not showing in the console, I should use these three texts to test it and
Here is my code,I don't know why the output is not showing in the console, I should use these three texts to test it and The outputs of these three tests must be stored in ExamStatFile.txt, to_testExamStatFile.txt, and exam_scoresExamStatFile.txt.
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner;
public class ExamStatistics {
// declaring variables final static int A = 90; final static int B = 80; final static int C = 65; final static int D = 55;
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub
int minScore = 100; // lowest score int maxScore = 0; // highest score int nextScore; // read from the file double sum = 0; double totalScores = 0; int low_Acounter = 0; int low_Bcounter = 0; int low_Ccounter = 0; int low_Dcounter = 0; int low_Fcounter = 0; double average; double psd; String outputMessage; String inputFileName; // allow the user to type on the console Scanner userInput = new Scanner(System.in); System.out.print("Enter the file name:"); inputFileName = userInput.nextLine(); File userFile = new File(inputFileName);
while (!userFile.exists()) { System.out.println("The file Numbers.txt is not found"); System.out.println("Enter the file name:"); inputFileName = userInput.nextLine(); } if (userFile.exists()) { Scanner readFile = new Scanner(userFile);
while (readFile.hasNextInt()) { nextScore = readFile.nextInt();// read the next score
if (nextScore >= 0 && nextScore <= 100) { totalScores++; // increase the total scores if (totalScores == 1) // if this is the first count then the next score in this round is the // first // score { // update minScore and maxScore to this score minScore = nextScore; maxScore = nextScore; } else { if (nextScore > maxScore) // if it is not the first score, update minScore and maxScore as // required maxScore = nextScore;// if nextScore is bigger than the maximum score, maximum score // which has a small value // assign the next score number which is a much bigger to maximum variable else if (nextScore < minScore) // if next score is smaller than minimum score, assign the minimum score to this // score which is a much smaller number minScore = nextScore;
}
// depending on the score read, update the counter for the appropriate grade if (nextScore >= low_Acounter) { // if next score is bigger than low A then count it low_Acounter++; } else if (nextScore >= low_Bcounter) {// if next score is bigger than low B then count it low_Bcounter++; } else if (nextScore >= low_Ccounter) { // if next score is bigger than low C then count it low_Ccounter++; } else if (nextScore >= low_Dcounter) { // if next score is bigger than low D then count it low_Dcounter++; } else { // if next score is bigger than low F then count it low_Fcounter++;
} // update the total counter sum += nextScore; // add the first score and when each iteration will add the next score to the // previous one } } readFile.close();
} if (totalScores > 0) // scores count>0 { average = sum / totalScores; // calculate average Scanner readeAgain = new Scanner(inputFileName); // re-read the file psd = 0; // a while loop to read till the end of file while (userInput.hasNextInt()) { nextScore = readeAgain.nextInt(); // read the next score // validate the score if (nextScore >= 0 && nextScore <= 100) // see if the score is valid { // if it is a valid score, calculate standard of deviation(psd) and update next // score each time and add the new psd to the old psd psd += Math.pow(nextScore - average, 2); } }
readeAgain.close(); // close the file psd = Math.sqrt(psd / totalScores); } else // no score in the range { average = 0; psd = 0; }
outputMessage = String.format("Total: %d Average score: %.2f Population standard deviation of the scores: %.2f" + " # of A, 85-100: %d%t%.2f%" + " # of B, 75-84: %d%t%.2f%" + " # of C, 65-74: %d%t%.2f%" + " # of D, 55-64: %d%t%.2f%" + " # of F, 00-54: %d%t%.2f%" + " Minimum score: %d" + " Maximum score: %d", totalScores, average, psd, low_Acounter, ((double) (low_Acounter * 100)) / totalScores, low_Bcounter, ((double) (low_Bcounter * 100)) / totalScores, low_Ccounter, ((double) (low_Ccounter * 100)) / totalScores, low_Dcounter, ((double) (low_Dcounter * 100)) / totalScores, low_Fcounter, ((double) (low_Fcounter * 100)) / totalScores, minScore, maxScore);
// create and open an output file PrintWriter writer = new PrintWriter("ScoreStatistics.txt"); writer.print(outputMessage); // output the message to file System.out.println(outputMessage); writer.close();
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started