Question: How would I go about finding min/max from array and what would the syntax look like?
((Edit: Started working on what it might look like (at the bottom of this post), but I'm not getting the syntax right))
Would like to create a method in my main class to find the students with the best and worst grades (and print) from an array of students, not sure about how the syntax would look with an array.
Main.java
import java.util.*;
import java.io.*;
public class Main
{
private static int i=0;
public static void main (String[] args) throws IOException
{
Student[] arrStudents = new Student[7];
Scanner fileInput = new Scanner(new File("students.txt"));
while (fileInput.hasNext())
{
String firstName = fileInput.next();
String lastName = fileInput.next();
int grade = fileInput.nextInt();
arrStudents[i] = new Student(firstName, lastName, grade);
i++;
}
getExcellentStudents(arrStudents);
getOKStudents(arrStudents);
getFailStudents(arrStudents);
System.out.println();
System.out.println("Total Number of Students: " + arrStudents.length);
}
public static void getExcellentStudents(Student[] arrStudents) throws IOException {
System.out.println("Excellent Students: ");
for(int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (grade > 89) {
System.out.println(arrStudents[i]);
}
}
System.out.println();
}
public static void getOKStudents(Student[] arrStudents) throws IOException {
System.out.println("OK Students: ");
for(int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (grade > 60 && grade < 90) {
System.out.println(arrStudents[i]);
}
}
System.out.println();
}
public static void getFailStudents(Student[] arrStudents) throws IOException {
System.out.println("Failure Students: ");
for(int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (grade < 61) {
System.out.println(arrStudents[i]);
}
}
System.out.println();
}
Student.java
public class Student {
private String firstName, lastName;
private int grade;
public Student (String firstName, String lastName, int grade)
{
this.firstName = firstName;
this.lastName = lastName;
this.grade = grade;
}
public String toString()
{
return firstName + " " + lastName + " " + grade;
}
public int getGrade()
{
return grade;
}
}
students.txt
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
Here's how the output should look (so I'm missing the last two lines right now):
Excellent Students:
John Smith 90
Barack Obama 95
OK Students:
Al Clark 80
Ann Miller 75
John Miller 65
Failure Students:
Sue Taylor 55
George Bush 58
Total Number of Students: 7
Student With Highest Grade: Barack Obama 95
Student With Lowest Grade: Sue Taylor 55
Easier to edit here than to post in the comments. I updated the post to make it a little more clear about what I'm asking.
Here's what I had so far for finding the max, but I'm still confused on what goes where exactly.
public void outputMaxMin(Student[] arrStudents) throws IOException {
Student bestStudent;
Student worstStudent;
student = new Student();
for (int i = 0; i < arrStudents.length; i++) {
int grade = arrStudents[i].getGrade();
if (bestStudent == null && worstStudent == null) {
bestStudent = student;
worstStudent = student;
continue;
}
if (grade > bestStudent.grade){
bestStudent = student;
}
if (grade < worstStudent.grade){
worstStudent = student;
}
}
}