# java Calculating the mean average

• 02-21-2014, 10:28 PM
danielki
java Calculating the mean average
I am trying to do the following java assignment and every thing seems to work fine except when I put a number<4 or >10 it prints both "Invalid grade!"
"You didn't enter any data!" what I wanted is to print only "Invalid grade!" I tried to fix it but I couldn't. Can any one help me please?

Thanks,

**Assignment:**
Create a program that asks for results of exams and calculates the mean average of the grades. Grades are floating point numbers between 4 and 10. Program asks for grades until a negative number is typed. If user gives a grade other than a number between 4 and 10, the text “Invalid grade!” will be printed on screen and program asks for another grade. Finally the program prints the number of inputted grades and their mean average on screen as shown in the example print. If no grades were inputted, the notice “You did not input any grades.” is the only thing printed on screen.

Hint:A double type variable is to be used to store the value of the average.

Program is written to a class called Average.

Example output

Program calculates the average of inputted grades.
Finish with a negative integer.

**Code**
Code:

``` import java.util.Scanner;     public class apples {                   public static void main(String[] args) {                     int inputNumber=0;            int sum;                    int count;                  double average;                                            sum = 0;           count = 0;                               Scanner reader = new Scanner(System.in);           System.out.println("Program calculates the average of inputted grades.");           System.out.println("Finish with a negative integer.");           System.out.print("Input a grade (4-10): ");           inputNumber = reader.nextInt();                                 while (inputNumber > 0 && inputNumber>=4 && inputNumber<=10) {             sum += inputNumber;              count++;                          System.out.print("Input a grade (4-10): ");             inputNumber = reader.nextInt();                       }                     if (inputNumber<4 || inputNumber>10) {                     System.out.println("Invalid grade!");           }                     if (count==0) {                   System.out.print("You didn't enter any data!");           }           else {             average = ((double)sum) / count;             System.out.println();             System.out.println(count + " grades inputted.");             System.out.print("Average of the grades: "+ average);           }           }           }```
• 02-21-2014, 11:21 PM
Norm
Re: java Calculating the mean average
[code]
[/code]
to get highlighting and preserve formatting.
• 02-21-2014, 11:40 PM
danielki
Re: java Calculating the mean average
Hello Norm,
How do I edit my post and wrap with code tags? I am new to the forum.
Is there a link that I have to follow?
• 02-21-2014, 11:54 PM
Norm
Re: java Calculating the mean average
Can you see an "Edit Post" in shaded bar below and to the right of your post? Click that.
• 02-22-2014, 03:28 AM
danielki
Re: java Calculating the mean average
thanks, I think I edited it correctly.
• 02-22-2014, 03:39 AM
Norm
Re: java Calculating the mean average
That looks better.

Normally the tests for valid data are done inside the loop. Execution does not exit the loop until the user has entered valid data or asked to quit the program. This code checks the data outside of the loop.