Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help returning (not printing) avg GPA from array??

807580Sep 5 2009 — edited Sep 10 2009
Here's what I have so far. After returning the value then I gotta print the avg in a separate method. I also need to check the names of the first and last student using an "equals" method. Please someone help with whatever you can. Here's what I got:
import java.util.Scanner;


public class StudentLists {
	
//Main method. Prompts user for the number of students.
   public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   System.out.print("Please enter the number of students in this class: ");
   int s = in.nextInt();
   Student[] students=new Student[s];
   FillStudents(s,students);
   printAll(students);
           }
     
   public static void FillStudents(int s, Student[] students){
   
   for (int i=0; i<s; i++) 
   {
   	Scanner inf = new Scanner(System.in);
   	System.out.println("Please enter student's first name: ");
    String first = inf.nextLine();
    System.out.println("Please enter student's last name: ");
   	String last=inf.nextLine();
   	System.out.println("Please enter the number of credits completed: ");
   	int c=inf.nextInt();
   	System.out.println("Please enter student's GPA: ");
   	Double g=inf.nextDouble();
   	students[i] = new Student(first, last, c, g);
   	  }
    }
    public static void printAll(Student[] students) {
    System.out.println("Names      Credits  GPA");
  	for(int i = 0; i < students.length; i++)
    System.out.println(students);
}
public static Double getAvg(Student[] students, Double g){
double sum=0.0;
double avg=0.0;
for(int i=0; i<students.length; i++){
sum+=g;
}
avg=sum/students.length;
return avg;
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 8 2009
Added on Sep 5 2009
131 comments
737 views