Search code examples
javaarraylistwhile-loopruntime-erroruser-input

How to get the average of all the numbers from the list


I need to get the average test score from the numbers the user submitted and keep getting infinity. I need to know the average test score. This is the student class:

public class Student {
    private String name;
    private int numOfQuizzes;
    private double totalScore;
    private ArrayList<Integer> scores;

    public Student(String name){
        this.name = name;
        scores = new ArrayList<Integer>();
    }

    public String getName() {
        return name;

    } public void addQuiz(int score){
        scores.add(score);
    }

    public double getTotalScore() {
        for(int score : scores){
            totalScore += score;
        }
        return totalScore;
    }

    public double getAverageScore(){
        return totalScore/(double)numOfQuizzes;
    }
}

This is what i have for the main:

    ArrayList<String> scores = new ArrayList<String>();
    Scanner inp = new Scanner(System.in);

    // Request the name
    System.out.print("What is your name? ");
    String name = inp.nextLine();

    // Create the student object
    Student student = new Student(name);

    // Ask for the grades
    System.out.print("Please enter your scores (q to quit): ");
    String grade = inp.nextLine();

    while (!grade.equals("q")) {
        // Convert the grade to an integer and pass it 
       **student.addQuiz(Integer.parseInt(grade));**
        //this is where the error is
        // Request a new grade
        grade = inp.nextLine();
    }

    // Report the results
    System.out.println("Students Name: " + student.getName());
    System.out.println("Total Quiz Scores: " + student.getTotalScore());
    double average = student.getTotalScore()/scores.size();
    System.out.println("Average Quiz Score: " + student.getAverageScore());

This is the Output:

What is your name? b
Please enter your scores (q to quit): 95
95
95
q
Students Name: b
Total Quiz Scores: 285.0
Infinity
Average Quiz Score: Infinity

As you can see I get infinity even when dividing the scores by the size.


Solution

  • You need to make few changes in your code as shown below:

    Student Class:

    public class Student {
        private String name;
        private double totalScore;
        private ArrayList<Integer> scores;
    
        public Student(String name){
            this.name = name;
            scores = new ArrayList<Integer>();
        }
    
        public String getName() {
            return name;
    
        } public void addQuiz(int score){
            scores.add(score);
        }
    
        public double getTotalScore() {
            totalScore = 0;
            for(int score : scores){
                totalScore += score;
            }
            return totalScore;
        }
    
        public double getAverageScore(){
            return getTotalScore()/scores.size();
        }
    }
    

    Main Class:

    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
    
            // Request the name
            System.out.print("What is your name?: ");
            String name = input.nextLine();
    
            // Create the student object
            Student student = new Student(name);
    
            // Ask for the grades
            System.out.print("Please enter your scores (q to quit): ");
            String grade = input.nextLine();
    
            while (!grade.equals("q")) {
                // Convert the grade to an integer and pass it 
               student.addQuiz(Integer.parseInt(grade));
                // Request a new grade
                grade = input.nextLine();
            }
    
            // Report the results
            System.out.println("Students Name: " + student.getName());
            System.out.println("Total Quiz Scores: " + student.getTotalScore());
            System.out.println("Average Quiz Score: " + student.getAverageScore());
        }
    
    }
    

    Try to use this code, It will work correctly and display average score instead of Infinity.

    Changes Done:

    1. Removed private int numOfQuizzes; from Student class.
    2. Added totalScore = 0; in getTotalScore() method in Student class.
    3. Added return getTotalScore()/scores.size(); in getAverageScore() method in Student class. Here getTotalScore() with return total score and scores.size() will return total number of scores. (3 in case of your example)
    4. Removed ArrayList<String> scores = new ArrayList<String>(); from Main class. As It is not needed in Main class.
    5. Removed double average = student.getTotalScore()/scores.size(); because student.getAverageScore() in next line will calculate and return average value.