views:

637

answers:

4

This is a lab I am working for for a CSE 201. The program is supposed to read information about students and their scores from a file, and output the name of each student with all his/her scores and the total score, plus the average score of the class, and the name and total score of the students with the highest and lowest total score.

The exact assignment can be seen here: http://www.cse.ohio-state.edu/cse201/labs/lab7/lab7.html

I'm having a little trouble getting it to compile, especially with the variable "students". Any help would be great, thanks so much everyone.

-Kyle

/* 
 * The program will read information about students and their
 * scores from a file, and output the name of each student with
 * all his/her scores and the total score, plus the average score
 * of the class, and the name and total score of the students with
 * the highest and lowest total score.
 */

import java.util.Scanner;

public class Lab7
{
        public static void main(String[] args)
        {
                // Input file name
                Scanner in = new Scanner(System.in);
                String filename = getFileName(in);

                // Input number of students
                int Student[students]  = getStudents(FileIOHelper.getNumberOfStudents(filename));

                // Input all students records and create Student array and
                // integer array for total scores
                int[] totalScores = new int[students.length];
                for(int i = 0; i < students.length; i++){
                        for(int j = 1; j < 4; j++){
                                totalScores[i] += students[i].getScore(j);
                        }
                }

                // Compute total scores and find students with lowest and
        // highest total score
                int maxIndex = 0, minIndex = 0;
                for(int i = 0; i < students.length; i++){
                        if(totalScores[i] > totalScores[maxIndex]){
                                maxIndex = i;
                        }else if(totalScores[i] < totalScores[minIndex]){
                                minIndex = i;
                        }
                }

                // Compute average total score
                int average = 0;
                for(int i = 0; i < totalScores.length; i++){
                        average += totalScores[i];
                }
                average /= students.length;

                // Output results
                outputResults(students, totalScores, maxIndex, minIndex, average);

        }

        // Given a Scanner in, this method prompts the user to enter
        // a file name, inputs it, and returns it.
        private static String getFileName(Scanner in)
        {
                System.out.print("Enter input file name: ");
                return in.nextLine();
        }

        // Given the number of students records n to input, this
        // method creates an array of Student of the appropriate size,
        // reads n student records using the FileIOHelper, and stores
        // them in the array, and finally returns the Student array.
        private static Student[] getStudents(int n)
        {
                Student[] student = new Student[n];
                for(int i = 0; i < student.length; i++){
                        student[i] = FileIOHelper.getNextStudent();

                }
                return student;
        }

        // Given an array of Student records, an array with the total scores,
        // the indices in the arrays of the students with the highest and
        // lowest total scores, and the average total score for the class,
        // this method outputs a table of all the students appropriately
        // formatted, plus the total number of students, the average score
        // of the class, and the name and total score of the students with
        // the highest and lowest total score.
        private static void outputResults(
                        Student[] students, int[] totalScores,
                        int maxIndex, int minIndex, int average
        )
        {
                System.out.println("\nName \t\tScore1 \tScore2 \tScore3 \tTotal");
                System.out.println("--------------------------------------------------------");
                for(int i = 0; i < students.length; i++){
                        outputStudent(students[i], totalScores[i], average);
                        System.out.println();
                }
                System.out.println("--------------------------------------------------------");
                outputNumberOfStudents(students.length);
                outputAverage(average);
                outputMaxStudent(students[maxIndex], totalScores[maxIndex]);
                outputMinStudent(students[minIndex], totalScores[minIndex]);
                System.out.println("--------------------------------------------------------");
        }

        // Given a Student record, the total score for the student,
        // and the average total score for all the students, this method
        // outputs one line in the result table appropriately formatted.
        private static void outputStudent(Student s, int total, int avg)
        {
                System.out.print(s.getName() + "\t");
                for(int i = 1; i < 4; i++){
                        System.out.print(s.getScore(i) + "\t");
                }
                System.out.print(total + "\t");
                if(total < avg){
                        System.out.print("-");
                }else if(total > avg){
                        System.out.print("+");
                }else{
                        System.out.print("=");
                }
        }

        // Given the number of students, this method outputs a message
        // stating what the total number of students in the class is.
        private static void outputNumberOfStudents(int n)
        {
                System.out.println("The total number of students in this class is: \t" + n);
        }

        // Given the average total score of all students, this method
        // outputs a message stating what the average total score of
        // the class is.
        private static void outputAverage(int average)
        {
                System.out.println("The average total score of the class is: \t" + average);
        }

        // Given the Student with highest total score and the student's
        // total score, this method outputs a message stating the name
        // of the student and the highest score.
        private static void outputMaxStudent(
                        Student student,
                        int score
        )
        {
                System.out.println(student.getName() + " got the maximum total score of: \t" + score);
        }

        // Given the Student with lowest total score and the student's
        // total score, this method outputs a message stating the name
        // of the student and the lowest score.
        private static void outputMinStudent(
                        Student student,
                        int score
        )
        {
                System.out.println(student.getName() + " got the minimum total score of: \t" + score);
        }
}
+3  A: 

The size of your array cannot be specified on the lefthand side.

The student array declaration should look like this:

int noOfStudents = FileIOHelper.getNumberOfStudents(filename);
//create an array of students of the given length
Student[] students = new Student[noOfStudents];
pjp
+1  A: 

This part looks to be an issue:

// Input number of students
int Student[students] = getStudents(FileIOHelper.getNumberOfStudents(filename));

You probably want to first get the number of students, then use that variable to call getStudents. Also, if you want the array to be called students it should not be in the brackets.

Student[] students = .....
ghills
+1  A: 

The line where you're having trouble is the very first line at the top of main():

int Student[students]  = getStudents(FileIOHelper.getNumberOfStudents(filename));

I always recommend reading through a problematic line with the same mindset as a compiler: it doesn't know anything except what you've told it, top to bottom, left to right.

So let's start at the beginning:

int

I know what an int is! You want to declare one! Awesome! Moving on...

Student

What the heck is a Student? I don't see anywhere in the code that would tell me what that is! As a human, I can infer that it's supposed to be the name of a class, since class names are always capitalized in Java, but it's not declared so the compiler cannot be sure. Is it perhaps supposed to be the name of this class (instead of Lab7)?

More importantly, if it is a class then you have just named two datatypes in a row: int and Student. Which type did you intend to use? Presumably if you want a list of Students, the int is not relevant at all. If you want the number of students, then just the int is relevant.

Moving on:

students

What the heck is a students? I again don't see anywhere in the code that would tell me what that is!

Let's back away for a moment. What are you really trying to do here? You're trying to obtain an array of all the students in the file. The getStudents() function presumably achieves that. (It may be buggy, but that's not our problem at the moment. We're just calling it here, so we will assume that it works.)

But how are you supposed to know how big to make the array if you haven't yet read it? Conveniently, you don't have to know! You can simply write:

Student[]

You are instantiating the array down in getStudents(), and there you've correctly given it a size. Here in main() you're simply declaring it, and no size is necessary.

Okay, so can you just write:

Student[] = getStudents(FileIOHelper.getNumberOfStudents(filename));

No, 'cause your variable doesn't have a name. How about something like this:

Student[] students = getStudents(FileIOHelper.getNumberOfStudents(filename));

I suspect that's what you intended, but since you got stuck somewhere along the way it's important to be able to get "unstuck," and mentally walking through what the compiler sees is a useful way to do that.

VoteyDisciple
That's true only with a strongly typed language.
Silence
Um... along the "think like a compiler" line: what the *heck* is an "int Student[]"??
Michael Borgwardt
It helps, I suppose, to have been using the right language. Edited accordingly.
VoteyDisciple
"int[] Student students" is still not valid syntax. The int doesn't belong in there (and putting the array brackets with the name is allowed in Java, but frowned upon). Still, +1 for the step-by-step introduction to the compiler's mindset.
Michael Borgwardt
Wow. It is clearly the end of the day, as I missed that entirely. Extensive edit forthcoming.
VoteyDisciple
Only goes to show that people are not really made to think like compilers :)
Michael Borgwardt
I don't downvote, but I would for not just saying "copy the array declaration from any of a billion online examples." Syntax may or may not make sense, but learning by copying is fine until the chips fall into place.
Yar
Copying from any of a billion online examples is a perfectly valid learning style, but nobody would need to ask Stack Overflow for that answer. The shortest response exhibiting requiring the least effort to compose is only sometimes the best.
VoteyDisciple
True is that! My only point was that syntax, unlike many other things in programming, is quite arbitrary hence you have to copy it. And the Java/C# syntax for an array declaration is particularly strange, I think. But yes, you're right that "go find an example and copy it" is probably not the most productive type of answer for SO.
Yar
+5  A: 

First of all: it's always useful to actually pay attention to the compiler error. Java's compier errors are very clear and useful most of the time, and tell you exactly what's wrong once you're learned to understand them. And generally, it's much easier for people here on SO to help you if you include the actual text of an error rather than saying "I have trouble getting it to compiler"

This is the first obviously wrong line:

int Student[students]  = getStudents(FileIOHelper.getNumberOfStudents(filename));

A variable declaration in Java consists of (slightly simplified):

  • The type of the variable
  • its name
  • optionally an assignment of an initial value

In the above line, you start with type int, but the next part Student[students] makes no sense - it looks like an array instantiation, certainly not a name. What you probably meant is:

Student[] students = getStudents(FileIOHelper.getNumberOfStudents(filename));

i.e. the type is Student[] (an array of Student objects), the name is 'students', and it's assigned the return value of the getStudents() method. An int is not involved anywhere (you don't have to specify the size of the array here, since it is created inside the getStudents() method).

Michael Borgwardt
I was halfway through typing the same answer
Pete Kirkham
Also known as the "The Fastest Gun in the West" problem :)
Michael Borgwardt
Obviously it wasn't obvious. ;-)
Dave Jarvis