Printing an array of grades

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3449
  • View blog
  • Posts: 15,618
  • Joined: 20-September 08

Re: Printing an array of grades

Posted 06 November 2017 - 02:56 AM

I see you haven't corrected your poor indentation. There are two lines that you can get rid of

Quote

System.out.println("Invalid grade, try again...");

(Redundant and wrong unless preceded by else)
...

Quote

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

(The user is telling you the grade so redundant)

Then your app will work
Was This Post Helpful? 0
  • +
  • -

#17 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 - 03:08 AM

View Postsnoopy11, on 06 November 2017 - 01:44 AM, said:

Yes as I said in post #10

use a for loop

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



Ah, sorry for the late update but I already got it working after looping the 2nd time, thank you both for that!
I have a whole new set of problems tho. It is in the original code I posted but for some odd reason, whenever I asked the user to give me an output from 0-100, it would say invalid output even if they put the right output?

Enter the number of students: 2
Please enter grade 1: 10.2
10.2
Invalid grade, try again...
Please enter grade 2: -1.5
-1.5
Invalid grade, try again...
Please enter grade 2: 100.5
100.5
Invalid grade, try again...
Please enter grade 2: 5.2
5.2
Invalid grade, try again...
Grades:10.2
Grades:5.2
The average is 7.70


Did I mess something up from the code?
Original code:
if ((grade >= LOWEST_GRADE)  && (grade <= HIGHEST_GRADE)){
    grades[i] = grade;
    sum      += grade;
    i++;
                
}System.out.println("Invalid grade, try again...");


Much thanks!
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6383
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Printing an array of grades

Posted 06 November 2017 - 03:36 AM

Why does it always print "Invalid grade..."? Please try to describe why this would happen.

It will help you to see why if you don't put the println statement on the same line as the closing brace.
Was This Post Helpful? 0
  • +
  • -

#19 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3449
  • View blog
  • Posts: 15,618
  • Joined: 20-September 08

Re: Printing an array of grades

Posted 06 November 2017 - 04:28 AM

Quote

Please try to describe why this would happen.

Clue: i've already covered that issue
Was This Post Helpful? 0
  • +
  • -

#20 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 07 November 2017 - 04:25 AM

View Postg00se, on 06 November 2017 - 04:28 AM, said:

Quote

Please try to describe why this would happen.

Clue: i've already covered that issue

Okay, I've fixed everything I needed to be fixed. I tried adding a code where if the user enters nothing, basically pressing the key "Enter", it will save the previous grades inputted for example,
Please enter grade 1:
27
Please enter grade 2:
100
Please enter grade 3:
2
Please enter grade 4:
41.5
Please enter grade 5:
13.98
Please enter grade 6:
<<user just presses return>>

Output after user enters blank line:
number of grades entered: 5

grade 1: 27
grade 2: 100
grade 3: 2
grade 4: 41.5
grade 5: 13.98

average grade: 36.896



This is my current code I have right now.
import java.util.Random;
import java.text.DecimalFormat;
import java.util.*;

public class test3{

   static final int LOWEST_GRADE = 0;
   static final int HIGHEST_GRADE = 100;    
   static final int SIZE = 10;
   public static void main ( String[] args ) {
   
   float[ ]grades;
   float sum = 0;
   int i = 0;
   double score = 0;
   boolean exit = false;
   
   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++;     
                            
            }else{
               System.out.println("That was not a valid percebtage, I need a number betweem 0 - 100" +"\n");
            }
        }
        for(i=0; i<grades.length; i++){
        System.out.printf("Grades:" + grades[i] + "\n");
        }
        System.out.printf("The average is %1$.2f\n", (sum / numStudents));
             
      
   }//ends class
   

}



Oops, I forgot to finish what "Enter" does. So it saves the previous input from the user and finalizes everything getting the average etc... How would I do that? Thank you
Was This Post Helpful? 0
  • +
  • -

#21 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1378
  • View blog
  • Posts: 4,320
  • Joined: 20-March 10

Re: Printing an array of grades

Posted 07 November 2017 - 05:03 AM

float grade = Gscanner.nextFloat();



You have this as your input ???

Its a float ??

How do you propose to test for the Enter Key which is a string ??

If you do want to test for an Enter key use a scanner which accepts a string and then convert the string to a float.
Was This Post Helpful? 0
  • +
  • -

#22 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3449
  • View blog
  • Posts: 15,618
  • Joined: 20-September 08

Re: Printing an array of grades

Posted 08 November 2017 - 03:39 AM

If you want to ensure that the user enter everything expected, you could loop until it's correct. Of course the user might need to quit the program entirely, which should also be catered for
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2