1

This program should take a user defined number, create an array of that size and let the user input the elements - which are grades - using a do..while loop. The program then needs to display all grades entered from lowest to highest, accumulate the grades, and find the average.

My output isn't displaying the entered grades correctly (if I enter 10,20,30, it displays 00,10,20) and I can't figure out what I'm doing wrong. Any help, please?

import java.util.Arrays;
import java.util.Scanner;

public class LoopArray
{
    public static void main(String[] arg)
    {
        Scanner keyboard = new Scanner(System.in);

        int count = 0;
        double totalAverage = 0;
        double gradesTotal = 0;

        System.out.println("Please input the number of grades you would like to submit for an average: ");
        int numberOfGrades = keyboard.nextInt();                 

        int[] studentScores = new int[numberOfGrades];

        do
        {
            System.out.println("Please enter grade for averaging: ");
            int inputGrade = keyboard.nextInt();
            count++;
            gradesTotal += inputGrade;

        } while (count < numberOfGrades);

        Arrays.sort(studentScores);

        for(count=0; count < studentScores.length; count++)
        {
           System.out.println("Grades entered were: " + count + studentScores[count]);
        }

        totalAverage = gradesTotal / numberOfGrades;
        System.out.println("The total of all grades entered is: " + gradesTotal);
        System.out.println("The average of grades entered is: " + totalAverage);

    }
}

4 Answers 4

1

Result

Grades entered were: 00
Grades entered were: 10
Grades entered were: 20

is generated with

System.out.println("Grades entered were: " + count + studentScores[count]);

So last number in each line is pair representing count + studentScores[count]. This means that:

  • 00 -> at position 0 array studentScores stores 0.
  • 10 -> at position 1 array studentScores stores 0
  • 20 -> at position 2 array studentScores stores 0

Which means you didn't fill your studentScores array with values from user.

Sign up to request clarification or add additional context in comments.

Comments

0

You are not putting any value inside the array. You need to add this to your do part of the loop studentScores[count] = inputGrade; Now your do while loop should look like this:

do
    {
        System.out.println("Please enter grade for averaging: ");
        int inputGrade = keyboard.nextInt();
        studentScores[count] = inputGrade;
        count++;
        gradesTotal += inputGrade;

    } while (count < numberOfGrades);

Also, inside your last for-loop, you are printing extra info. Just remove that count from System.out.println

 System.out.println("Grades entered were: " + studentScores[count]);

Anything you don't understand let me know thanks

1 Comment

That was it! Thanks so much! I was getting really frustrated. I appreciate it!
0

Because count start from 0. You should check it.

Comments

0

you forgot to populate the array using the gardes entered

 do
    {
        System.out.println("Please enter grade for averaging: ");
        int inputGrade = keyboard.nextInt();
        studentScores[count]=inputGrade;
        count++;
        gradesTotal += inputGrade;

    } while (count < numberOfGrades);

4 Comments

It's not letting me upvote you or @FarazDurrani but thanks a lot!
@Jerstern You need 15 reputation to get up-voting privilege. Anyway you can cast your votes later when you will get this amount of reputation (it is not that hard to get since each day there are thousands new of questions here:)
@FarazDurrani Another question on meta: Is it reasonable to flag a comment asking for upvotes?.
Definitely will once I hit 15. I'm still trying to grasp java at this point. As soon as it seems easy, a curve ball gets thrown, ha. Such is life. Thanks @Pshemo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.