Java method repeats output incorrectly and too often
I was just trying to get suggestions for the course advice from a StackOverflow mentor. I apologize.
/**
* prints a student’s average grade by student ID
* @param (String studentID)/ */
public static void print_average_grade(String studentID)
{
// Here, you have studentID passed into the function but you are not using it
for (Student s : studentList)
{
// Use the studentID here!
if(s.getStudentID().equals(studentID)) {
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
}
}
Use the studentID
passed as an argument so that you print middle grade for the correct student every time.
You use two loops, first you use
for (Student s : studentList) {
print_average_grade(s.getStudentID());
}
to invoke print_average_grade
by passing it on studentID
, which you immediately ignore and then iterate over all the students, again ...
public static void print_average_grade(String studentID) {
for (Student s : studentList) {
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
}
Change the method instead print_average_grade
to accept Student
and just compute the average for the specified student
public static void print_average_grade(Student s) {
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
Then in your main loop, just pass it Student
which one you want ...
for (Student s : studentList) {
print_average_grade(s);
}
If you "really" need to do it with id
, then I would write a method that int
takes a value (id) and searches for an object Student
, since you think you want to do this multiple times ... (of course, you will be useful Map
)
public Student getStudentById(int id) {
Student match = null;
for (Student s : studentList) {
if (s.getStudentID() == id) {
match = s;
break;
}
}
return match;
}
In the mean printing tool, you do not use the student ID that you pass to filter the list. You iterate over the entire list and print the average each time. But when printed, its student ID that was passed to the function is used and the average of all students will be shown against the same ID.
In print_average_grade
, wrap the logic to calculate the mean and print in a condition that will only do so if the student ID s
matches studentId
.
public static void print_average_grade(String studentID) {
for (Student s : studentList) {
if (!s.getStudentID().equals(studentID)) continue;
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
}
Alternatively, you can create a method to "find" the student with that ID, and then pass only that student to print_average_grade
. Or you can store students in Map<String, Student>
, where the key is the student ID, so you can find the student with map.get(studentID)
.
Changing print_average_grade (String studentID) to print_average_grade (student-student) would be solved like this:
for (Student s : studentList)
print_average_grade(s);
public static void print_average_grade(Student student) {
double total = student.getGrade1() + student.getGrade2()
+ student.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + student.getStudentID() + " "
+ average);
}