So I need to make my program print the total number of students, the average grade, and both the high and low grade through the array which I created through the text file attached. Can someone help me understand how to do this? What I have so far is below:
import java.util.Scanner;
import java.io.*;
public class Student
{
String fname, lname, number, status;
int grade;
public Student(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
this.status = getStatus(grade);
}
public String getStatus(int grade)
{
String status = null;
if (grade > 89)
status = "Excellent";
if (grade >= 60 && grade <= 89)
status = "Okay";
if (grade < 60)
status = "Failure";
return status;
}
public String toString(){
return fname + " " + lname + " " + grade;
}
}
import java.util.Scanner; //imports Java Scanner
import java.io.*;//imports File Scanner
public class Grades //Name of Class
{
public static void main (String[] args) throws IOException
{ String first_name, last_name;
int grade = 0, total=0, count=0, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
int arrCtr = 0;
Student st[] = new Student[4] ;
double average = 0;
Scanner fileInput = new Scanner(new File("students.txt"));
while (fileInput.hasNext())//scans file
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
st[arrCtr] = new Student(first_name,last_name,grade);
arrCtr = arrCtr + 1;
}
count = arrCtr + 1;
System.out.println ("Students with excellent grades");
for(int i=0; i<arrCtr; i++)
{
if (st[i].status == "Excellent") {
System.out.println(st[i].toString() );
}
} // end for
System.out.println ("Students with okay grades");
for(int i=0; i<arrCtr; i++)
{
if (st[i].status == "Okay")
System.out.println(st[i].toString() );
}
System.out.println ("Students with failing grades");
for(int i=0; i<arrCtr; i++){
if (st[i].status == "Failure")
System.out.println(st[i].toString() );
}
}
}
Any help would be greatly appreciated.