Home > Back-end >  Code is supposed to count amount of grades from text file (27 grades) by adding an increment to the
Code is supposed to count amount of grades from text file (27 grades) by adding an increment to the

Time:07-10

Output comes up as 27 A's, 0 B's-F's. Can anyone help me to fix the code so that it reads the grades correctly? The text file is just: 90 78 67 88 33 94 100 18 74 82 60 62 83 91 88 55 42 76 91 80 70 40 98 , with one grade per line

package grades;
import java.io.*;
import java.util.Scanner;

public class grades {
    public static void main(String[] args)throws FileNotFoundException {
        
    File myFile = new File("grades.txt");
    Scanner inputFile = new Scanner(myFile);

    int gradeA = 0;
    int gradeB = 0;
    int gradeC = 0;
    int gradeD = 0;
    int gradeF = 0;
    
    while(inputFile.hasNext()) 
    {
            int grade = inputFile.nextInt();
        if(grade >= 90 || grade <= 100)
            gradeA  ;
        else if(grade >= 80 || grade <= 89)
            gradeB  ;
        else if(grade >= 70 || grade <= 79)
            gradeC  ;
        else if(grade >= 60 || grade <= 69)
            gradeD  ;
        else if(grade <= 59)
            gradeF  ;
    }
    inputFile.close();
    
    System.out.println("Here are the amount for each Letter Grade:");
    System.out.println("\tA: " gradeA);
    System.out.println("\tB: " gradeB);
    System.out.println("\tC: " gradeC);
    System.out.println("\tD: " gradeD);
    System.out.println("\tF: " gradeF);
    
    }
}

CodePudding user response:

Start with the highest priority if statement or use the && operands

`public class grades {
public static void main(String[] args)throws FileNotFoundException {
    
File myFile = new File("grades.txt");
Scanner inputFile = new Scanner(myFile);

int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
int gradeF = 0;

while(inputFile.hasNext()) 
{
        int grade = inputFile.nextInt();
    if(grade <= 59)
        gradeF  ;
    else if(grade >= 60 || grade <= 69)
        gradeD  ;
    else if(grade >= 70 || grade <= 79)
        gradeC  ;
    else if(grade >= 80 || grade <= 89)
        gradeB  ;
     else
        gradeA  ;
   
    
    
}
inputFile.close();

System.out.println("Here are the amount for each Letter Grade:");
System.out.println("\tA: " gradeA);
System.out.println("\tB: " gradeB);
System.out.println("\tC: " gradeC);
System.out.println("\tD: " gradeD);
System.out.println("\tF: " gradeF);

}
}

`

public class grades {
public static void main(String[] args)throws FileNotFoundException {

File myFile = new File("grades.txt");
Scanner inputFile = new Scanner(myFile);

int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
int gradeF = 0;

while(inputFile.hasNext()) 
{
        int grade = inputFile.nextInt();
   
   if(grade >= 90 && grade <= 100)
        gradeA  ;
    else if(grade >= 80 && grade <= 89)
        gradeB  ;
    else if(grade >= 70 && grade <= 79)
        gradeC  ;
    else if(grade >= 60 && grade <= 69)
        gradeD  ;
    else if(grade <= 59)
        gradeF  ;
    
    
}
inputFile.close();

System.out.println("Here are the amount for each Letter Grade:");
System.out.println("\tA: " gradeA);
System.out.println("\tB: " gradeB);
System.out.println("\tC: " gradeC);
System.out.println("\tD: " gradeD);
System.out.println("\tF: " gradeF);

}
}

CodePudding user response:

Here are some things to fix improve your code:

  1. Replace the "or" operator (||) with the "and" operator (&&), because your intention is that both of the things are true. In the first "if" statement, you want "grade" to be at least 90, and 100 or less. When using "or", any integer value would pass. Is "grade" 1 million? Cool, that passes grade >= 90. Is it negative 20,000? That passes the other side of the "or" statement (grade <= 100). To fix it, change this:
    if (grade >= 90 || grade <= 100)
    
    to this:
    if (grade >= 90 && grade <= 100)
    
  2. Simplify the logic inside each "if" statement. It isn't necessary to verify that "grade" is both "less than X" and "greater than Y". You just need one comparison. Anything 90 or up is an A. 110? Sure, that's an A. Once your code has checked a value for being "90 or higher", the next check needs to check only against 80. It isn't necessary to also check that it's 89 or less – it must be 89 or less, because if an integer were any larger than 89 it would be at least 90, and thus would not have made it past the first "if" statement. So change the various "if" statements from this:
    if (grade >= 90 || grade <= 100)
        gradeA  ;
    else if (grade >= 80 || grade <= 89)
        gradeB  ;
    ...
    
    to this:
    if (grade >= 90)
        gradeA  ;
    else if (grade >= 80)
        gradeB  ;
    ...
    

Here's a simpler version of your program (using an int array instead of reading from a file), and applying those changes above, with the output below:

int[] grades = {90, 78, 67, 88, 33, 94, 100, 18, 74, 82, 60,
        62, 83, 91, 88, 55, 42, 76, 91, 80, 70, 40, 98};
int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;
for (int grade : grades) {
    if (grade >= 90)
        gradeA  ;
    else if (grade >= 80)
        gradeB  ;
    else if (grade >= 70)
        gradeC  ;
    else if (grade >= 60)
        gradeD  ;
    else
        gradeF  ;
}
System.out.println("A: "   gradeA);
System.out.println("B: "   gradeB);
System.out.println("C: "   gradeC);
System.out.println("D: "   gradeD);
System.out.println("F: "   gradeF);

A: 6
B: 5
C: 4
D: 3
F: 5
  • Related