user2129954
user2129954

Reputation:

Calling a list as an array from a .txt and need to display an average

The .txt file looks like this:

Gator Ali 85
Vator Ella 75
Beam James 95
Lastin Class 55

I've seen other people trying to get similar code on here, but none of that helped.
I can get the average; however, I also need to print out if the score 10 points below average.
Here is my output: run:

Welcome to the Student Scores Application.

Ali Gator 85
Ella Vator 75
James Beam 95
Class Lastin 55
Beam, James: 95
Gator, Ali: 85
Lastin, Class: 55
Vator, Ella: 75

Your score is 10 points or more below the class average

Class Average: 77.5

Here is my code:

public static void main(String[] args) throws Exception {

    Scanner aScanner = new Scanner(new FileReader("src//chapt11//Studentdata.txt"));
    System.out.println("Welcome to the Student Scores Application.\n");

    int nStudent = 100;
    Student[] studentArray = new Student[nStudent];
    int counter = 0;
    while (aScanner.hasNext()) {
        String lastName = aScanner.next();
        String firstName = aScanner.next();
        int score = aScanner.nextInt();
        System.out.println(firstName + " " + lastName + " " + score);
        studentArray[counter++] = new Student(lastName, firstName, score);  
        Arrays.sort(studentArray, 0, counter); 
    } 

    System.out.println();

    for(int j = 0; j < counter; j++){  
        System.out.println(studentArray[j]);
    }

    double sum = 0;
    for(int j = 0; j < counter; j++){    
        sum += studentArray[j].getExamScore();
    }
    double average = (sum*1.0)/counter;

    for(int j = 0; j < counter; j++){    
        int score = studentArray[j].getExamScore();
        if (score <= (average - 10)){
             System.out.println("Your score is 10 points or more below the class average");
        } 
    }
    System.out.println();
    System.out.println("Class Average: " + average);

    System.out.println();
    aScanner.close();  
}

class Student implements Comparable<Student> {
private String firstName;
private String lastName;
private int score;

public Student(String firstName, String lastName, int score) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.score = score;
}

public int getExamScore() {
    return score;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

@Override
public int compareTo(Student s) {
    if(!firstName.equalsIgnoreCase( s.firstName)) {
        return firstName.compareToIgnoreCase(s.firstName);
    }
    if(!lastName.equalsIgnoreCase(s.lastName)) {
        return lastName.compareToIgnoreCase(s.lastName);
    }
        return score - s.score;
    }
 @Override 
 public String toString(){ 
        return firstName + ", " + lastName + ": "+ score;
    }
}

Upvotes: 0

Views: 94

Answers (1)

user2030471
user2030471

Reputation:

I see some problems with your code.

Firstly, there is no need to call Arrays.sort every time you add a Student to the studentArray. Place this method call outside the end of while which sorts complete array with just one call to the method.

Secondly, and more importantly, how do you want the Student objects compared?

The compareTo method inside Student doesn't make sense. If you want the students to be sorted only by their scores you should have something like this:

@Override
public int compareTo(Student s) {
    return this.score - s.score;
}

And as far as the expected output is concerned, this is what's happening.

The following line prints each Student as they are added to the array:

System.out.println(firstName + " " + lastName + " " + score);

This part prints each Student object again:

for (int j = 0; j < counter; j++) {
    System.out.println(studentArray[j]);
}

And this line gets printed only when score <= (average - 10) evaluates to true:

System.out.println("Your score is 10 points or more below the class average");

I believe now you see the problem.

Upvotes: 1

Related Questions