Printing an array of grades

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 580 Views - Last Post: 08 November 2017 - 03:39 AM Rate Topic: -----

#1 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Printing an array of grades

Posted 04 November 2017 - 02:05 AM

Hello again folks, I seem to be having a problem with my coding. I'm not sure why my code doesn't print as my goal to basically print grades using arrays.
public class test{

   static final int LOWEST_GRADE = 0;
   static final int HIGHEST_GRADE = 100;    
   
   public static void main ( String[] args ) {
   
   int[ ]grades;
   double sum = 0;
   int i = 0;
   String aGradesAverage = "";
   Scanner Gscanner = new Scanner(System.in);
   
   int numStudents = Gscanner.nextInt();
   
   System.out.println(numStudents);
   
   
   if (numStudents <= 0) {
            System.out.println("Invalid number of students.");

        }        
        grades = new int[numStudents];
        
        while (i < numStudents)
        {
            System.out.printf("Enter the grade for student %1$d: ", (i++));
            int grade = Gscanner.nextInt();
            System.out.println(grade);
            
            // chek if grade is between 0 and 100
            if ((grade >= LOWEST_GRADE) && (grade <= HIGHEST_GRADE)) {
                grades[i] = grade;
                sum      += grade;
                i++;
            }
            System.out.println("Invalid grade, try again...");
        }
        System.out.printf("The average is %1$.2f\n", (sum / numStudents));
             
      
   }//ends class
   

}


Can someone tell me what I did wrong and why I cannot print what I want it to? Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing an array of grades

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,615
  • Joined: 20-September 08

Re: Printing an array of grades

Posted 04 November 2017 - 03:28 AM

You're incrementing i in two places. Only increment it in the second place. You need an initial prompt or your user won't know what to enter at the outset. Also, your if statements are wrong - probably as a result of poor indentation

It won't help you (nor will it help people here to help you) that your code is badly indented/formatted. See
http://technojeeves....-code-formatter

Your naming also needs attention - class names begin uppercase, variables lowercase
http://technojeeves....yle-conventions

This post has been edited by g00se: 04 November 2017 - 04:53 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 04 November 2017 - 03:47 AM

to further clarify...

you have this...

 System.out.printf("\nEnter the grade for student %1$d: ", (i++));


which is incrementing i.... unnecessarily ...

you needed to do this

 System.out.printf("\nEnter the grade for student %1$d: ", (i+1));


which adds 1 to i to give appropriate student numbers without changing the value of i.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,615
  • Joined: 20-September 08

Re: Printing an array of grades

Posted 04 November 2017 - 04:56 AM

Also (minor point), don't use platform-specific line breaks. You want
not

Quote

System.out.printf("\nEnter the grade for student %1$d: ", (i+1));

but
System.out.printf("%nEnter the grade for student %1$d: ", (i+1));

Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 04 November 2017 - 07:37 AM

Ok, Thanks for the tip !!

%n it is...
Was This Post Helpful? 0
  • +
  • -

#6 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Re: Printing an array of grades

Posted 05 November 2017 - 10:48 PM

Ah, I seem to have it working again, sorry about the late reply, I was busy during the weekend. Also, I am pretty sure I've learned how to do this before but I forgot, I wanted to print the output of what the user has typed? For example, if I ask for 10 grades, it will print all 10 grades and give the average in advance on the output. How would I do that again? Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 05 November 2017 - 11:12 PM

To get an average of 10 grades...

ask for 10 grades.. total the grades up and divide by 10.
Was This Post Helpful? 0
  • +
  • -

#8 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Re: Printing an array of grades

Posted 05 November 2017 - 11:33 PM

View Postsnoopy11, on 05 November 2017 - 11:12 PM, said:

To get an average of 10 grades...

ask for 10 grades.. total the grades up and divide by 10.


System.out.println("Please enter grade "+ (counter +1)+ " or press enter when you are done");
sTemp = sc.nextLine();


Ah, sorry if I didn't explain myself clearly, I already have the code to do that. What I want is the output for the user. For example, if the user enters 10.5, 5.6, 7.6, 5.2. It will bring the output

Grades: 10.5
Grades: 5.6
Grades: 7.6
Grades: 5.2
Average: 7.22

Something like this, I'm pretty sure I've learned this but I can't seem to remember how to do this. Thank you!
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 06 November 2017 - 12:02 AM

Well,

where is the code where you store the individual grades are you using an array or what ?

Also what is your total variable named as....
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 06 November 2017 - 12:16 AM

Sorry forgot about your original post #1,

to do what you ask..

System.out.printf("Grades: %f", grades[i]);


in a for loop from 0 to <numStudents then

System.out.printf("%nAverage: %f", sum/numStudents);


you would need to change sum and grades to float however if your input is 10.5 etc because int does not handle floating point numbers.
Was This Post Helpful? 0
  • +
  • -

#11 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Re: Printing an array of grades

Posted 06 November 2017 - 12:34 AM

I changed the sum and grades to float, however I got an exception, "ArrayIndexOutOfBoundsException".
public class test{

   static final int LOWEST_GRADE = 0;
   static final int HIGHEST_GRADE = 100;    
   
   public static void main ( String[] args ) {
   
   float[ ]grades;
   float sum = 0;
   int i = 0;
   double score = 0;
   
   String aGradesAverage = "";
   Scanner Gscanner = new Scanner(System.in);
   
   System.out.print("Enter the number of students: ");
   int numStudents = Gscanner.nextInt();
   
   
   
   if (numStudents <= 0) {
            System.out.println("Invalid number of students.");

        }        
        grades = new float[numStudents];
        
        while (i < numStudents)
        {
            System.out.printf("Please enter grade %1$d: ", (i+1));
            float grade = Gscanner.nextFloat();
            System.out.println(grade);
            
            // check if grade is between 0 and 100
            
            
            if ((grade >= LOWEST_GRADE)  && (grade <= HIGHEST_GRADE)){
                grades[i] = grade;
                sum      += grade;
                i++;

            }
            System.out.println("Invalid grade, try again...");
        }
        System.out.printf("Grades: %f", grades[i]);

        System.out.printf("The average is %1$.2f\n", (sum / numStudents));
             
      
   }//ends class
   

}



Did I do anything wrong in this? Many thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,775
  • Joined: 12-December 12

Re: Printing an array of grades

Posted 06 November 2017 - 12:43 AM

Your while loop while (i < numStudents) will terminate when i has the value numStudents. You then attempt to read the value of grades[i], i.e. grades[numStudents], which is beyond the end of the array.
Was This Post Helpful? 0
  • +
  • -

#13 blackmask85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 17

Re: Printing an array of grades

Posted 06 November 2017 - 12:53 AM

View Postandrewsw, on 06 November 2017 - 12:43 AM, said:

Your while loop while (i < numStudents) will terminate when i has the value numStudents. You then attempt to read the value of grades[i], i.e. grades[numStudents], which is beyond the end of the array.

Oh I see, any solutions to fix this problem? My friend gave me this problem and it sounded really simple but it is actually really difficult unless I'm just complicating things for myself.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,775
  • Joined: 12-December 12

Re: Printing an array of grades

Posted 06 November 2017 - 12:55 AM

What solution(s) occur to you? Do you understand the problem?

Note that you currently only have a single loop. If you want to accept the input values, and subsequently output these values, then you need two loops.
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 06 November 2017 - 01:44 AM

Yes as I said in post #10

use a for loop

for(i =0; i<numStudents; i++)
System.out.printf("Grades: %f", grades[i]);



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2