-1

The program I am working on takes numbers from a plain text file, and does calculations based on the numbers. One of the calculations needed is the overall minimum value.

The issue lies in the fact that my current minimum score variable is set to equal the current-selected value from the text document. So, it basically is always going to be equal to the last value. Or, alternatively it is always equal to zero if I do not set the two values to be equal to each other. The trouble is, I have no idea how to accurately calculate the minimum value.

Java Code

public class ExamStats {
static final int bottomA = 90;
static final int bottomB = 80;
static final int bottomC = 70;
static final int bottomD = 60;

public static void main(String[] args) throws FileNotFoundException {
    double totalSum = 0;
    int numberOfExams = 0;
    double examAverage = 0;
    int currentScore = 0;
    double aCount = 0;
    double bCount = 0;
    double cCount = 0;
    double dCount = 0;
    double fCount = 0;
    double aPercent = 0;
    double bPercent = 0;
    double cPercent = 0;
    double dPercent = 0;
    double fPercent = 0;
    int maxScore = 0;
    int minScore = 0;

    Scanner scan = new Scanner(System.in);
    File f = new File("Grades.txt");
    DecimalFormat twoDecimal = new DecimalFormat("#.##");
    DecimalFormat noDecimal = new DecimalFormat("#");
    System.out.println("Enter the name of the file here: ");
    String fileName = scan.nextLine();
    Scanner fileLocation = new Scanner(new FileReader(fileName));
    while (fileLocation.hasNext()){             //Reads each individual entry from the text file. 
        currentScore = fileLocation.nextInt();
        numberOfExams++;
        totalSum += currentScore;
        if (currentScore >= bottomA){
            aCount++;
        }
        if (currentScore >= bottomB && currentScore < bottomA){
            bCount++;
        }
        if (currentScore >= bottomC && currentScore < bottomB){
            cCount++;
        }
        if (currentScore >= bottomD && currentScore < bottomC){
            dCount++;
        }
        if (currentScore < bottomD) {
            fCount++;
        }

        if (currentScore < minScore){
            minScore = currentScore;
        } 
        if (currentScore > maxScore){
            maxScore = currentScore;
        }

2 Answers2

0

minScore is being assigned to currentScore

minScore = currentScore;

Then it is compared, but both variables will have the same value, the condition will never be true

if (currentScore < minScore){
    minScore = currentScore;
}

Remove the assignment before the if and the program will work as expected.

Carlos Monroy Nieblas
  • 2,225
  • 2
  • 16
  • 27
  • But if all of the values are <0, min score will always equal 0, since this is what it is initialized to. – Matt Oct 20 '16 at 00:50
  • Could you provide an example of a negative number that will evaluate false to `if ( < 0)` ? – Carlos Monroy Nieblas Oct 20 '16 at 06:17
  • My bad, I put the wrong arrow in. The minScore will be correct if at least one negative number is inputted, and the max will be correct if there is at least one positive number inputted. If these conditions can't be guaranteed, you will have to initialize the variables to the first score in the file – Matt Oct 20 '16 at 09:33
0

The problem is that you set the minimum score to the current score every iteration of the loop with the line

minScore = currentScore;

If you change the program so that this line is only ever executed once, before the loop. This also means that you'll have to check that there is actually data before doing this.

While it will work for non-negative numbers, it might also be worth putting the line maxScore = currentScore in the same place, to avoid issues if all of the scores are less than 0.

Edit: The following code 'should' do what I'm talking about, but there are probably better ways of implementing it

//Rest of your program
System.out.println("Enter the name of the file here: ");
String fileName = scan.nextLine();
Scanner fileLocation = new Scanner(new FileReader(fileName));
if (fileLocation.hasNext()) {
    currentScore = fileLocation.nextInt();
    minScore = currentScore;
    maxScore = currentScore;
} else {//Throw an error or something
}
// Rest of the program

You would then have to change the loop a little to accommodate this. Changing the loop to a post-test one, and moving the line currentScore = fileLocation.nextInt(); to the last line of the loop would be one way of making it work.

Matt
  • 968
  • 2
  • 13
  • 22
  • That's actually not supposed to be there, it's now removed. However, the main issue is still not solved, as minScore is equal to 0 be default; and Java will not let me have an non-initialized variable for minScore, since it's being used in a comparison. – Seth Snider Oct 20 '16 at 00:52
  • Hence why I stated that instead of deleting the line, you should find a way of moving its functionality to above the loop, so that minScore is initialized to the first value in the file – Matt Oct 20 '16 at 00:53
  • I replied to the wrong person. Alright, I will try equating it before the loop. – Seth Snider Oct 20 '16 at 00:55