import java.util.Scanner;

public class AverageArray {
    public static void main(String[] args) {
    final int TOTAL_NUMBERS = 10;
    int[] numbers = new int[TOTAL_NUMBERS];
    float sum;

    //Create a Scanner
    Scanner input = new Scanner(System.in);

    //Read all numbers
    for (int i = 0; i < numbers.length; i++) {
        System.out.print("Enter a number:");

    //Conver String into integer
    numbers[i] = input.nextInt();
    }

    //Find the sum and average

    for (int i = 0; i < numbers.length; i++){
     sum += numbers[i];

    int average = sum / TOTAL_NUMBERS;

    }

    //Find the number of values in the array greater than the average 
    int maxAverage = 0;
    for (int i = 0; i < numbers.length; i++) {
        if (numbers[i] > average) maxAverage++;

    }

    //Prepare the result
    String output = "The array is";
    for (int i = 0; i < numbers.length; i ++) {
        output += numbers[i] + " ";
    }

    output += "\nThe average is" + average;
    output += "\nThe number of values in the array greater than the average" + "is" + maxAverage;

    //Display the result
    System.out.println(output);

    }
}

Error:

AverageArray.java:25: possible loss of precision
found   : float
required: int
    int average = sum / TOTAL_NUMBERS;
                      ^
AverageArray.java:32: cannot find symbol

Dani AI

Generated

Short summary of what went wrong and how to fix it.

The compile errors are coming from three separate issues that often appear together in small programs:

  • "possible loss of precision" — a floating-point expression (float/double) was being assigned to an int variable without an explicit cast. The division result should be stored in a floating type if fractional precision is wanted.
  • "illegal start of type" / "') expected'" — this normally means a brace or parenthesis is mismatched earlier in the file so the compiler sees the for header in the wrong place. Check the lines immediately before the reported error.
  • "cannot find symbol" for average — usually a scope problem: average was declared inside a block (for loop) but later referenced outside that block.

What to apply (concise, practical):

  • Initialize accumulators (local variables must be initialized): set the sum to 0 before using sum += ....
  • Do accumulation in the loop, then compute the average after the loop (declare average in the same scope where it will be used).
  • Decide whether fractional average is required. Prefer double for better precision; cast only when intentionally truncating/rounding.
  • If rounding to an integer is required, use explicit rounding: int rounded = (int) Math.round(average); (note: Math.round(float) returns int; Math.round(double) returns long).
  • If counting "above average", use the unrounded average for the comparison unless the problem definition explicitly wants comparison against a rounded value (then adjust to >= or > accordingly).

Modern, compact option (Java 8+):

double avg = Arrays.stream(numbers).average().orElse(0.0);
long countAbove = Arrays.stream(numbers).filter(n -> n > avg).count();

Quick troubleshooting checklist:

  • Count opening/closing braces or use an IDE to highlight matching braces.
  • Confirm sum is initialized.
  • Move average calculation outside the accumulation loop.
  • Match the variable type (int vs float/double) to the intended semantics.
  • Print intermediate values or run with a small test array to confirm logic.

Acknowledgement: and correctly pointed out moving average out of the loop and initializing the sum; raised the important distinction between truncation and proper rounding.

Recommended Answers

All 9 Replies

//Find the sum and average

          for (int i = 0; i < numbers.length; i++){
          sum += numbers[i];
          int average = sum / TOTAL_NUMBERS;
          }

Here you are making the average an integer, when in reality it should be a float so that you have the correct precision.

you can either:
a) change "int average = sum / TOTAL_NUMBERS;" to "float average = sum / TOTAL_NUMBERS;"
b) if you really want average to be rounded to an int, type cast the right side of the equals. "int average = (int) sum/TOTAL_NUMBERS;"

so what you should really have looks like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = sum / TOTAL_NUMBERS;

JamesCherrill:

... and put line 5 outside the loop!

cant believe i let that slip. thanks james

... and put line 5 outside the loop!

JamesCherrill ok so i put line 5 outside the loop float average = sum / TOTAL_NUMBERS; i think that worked but now im still having issues getting the number of value greater than the average i think i totally have the wrong method...

//Find the number of values in the array greater than the average 
int maxAverage = 0;
for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] > average) maxAverage++;

}

Errors:

AverageArray.java:32: illegal start of type
    for (int i = 0; i < numbers.length; i++) {
    ^
AverageArray.java:32: ')' expected
    for (int i = 0; i < numbers.length; i++) {
              ^

Not enough code to tell, but looks like a mis-matched bracket shortly before the line where you get the error.


b) if you really want average to be rounded to an int, type cast the right side of the equals. "int average = (int) sum/TOTAL_NUMBERS;"

so what you should really have looks like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = sum / TOTAL_NUMBERS;

Casting is not a good idea for rounding numbers to int. If you use the code above (casting), it will not round your average to the nearest int but rather it will truncate the resulting number and drop the decimal places. A better and accurate way to do it is to use Math.round(float average) which will actually round your average to the nearest int.
Example:
let's say average = 5.9
by casting to int:
average = 5
by using Math.round:
average = 6

your code should be like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = Math.round(sum / TOTAL_NUMBERS);

Seriously - if you can't figure out how to use code tags then read the stickies at the top of the forum that explain in detail how to do so. Or you could just quote one of the other members in here (such as Jocamps) who used code tags in their post, and it would show you exactly how to do it.

you guys have been very helpful I've tried to work around this error but its not helping i dont see where i am getting an illegal start the brackets seem to be in place. but then i'm questionable on my formula of obtaining the number of values in array greater than the average any ideas???

}
   
   //Find the number of values in the array greater than the average 
   int maxAverage = 0;
   for (int i = 0; i < numbers.length; i++) {
      if (numbers[i] > average) maxAverage++;
      
   }

AverageArray.java:32: illegal start of type
for (int i = 0; i < numbers.length; i++) {
^
AverageArray.java:32: ')' expected
for (int i = 0; i < numbers.length; i++) {
^
AverageArray.java:32: illegal start of type

try posting your whole code

I tested your code that you posted above with the fix I suggested earlier and it is working fine. Perhaps you accidentally deleted or added an extra bracket. Here it is though:

import java.util.Scanner;

public class AverageArray {
	public static void main(String[] args) {
		final int TOTAL_NUMBERS = 10;
		int[] numbers = new int[TOTAL_NUMBERS];
		float sum = 0;

		// Create a Scanner
		Scanner input = new Scanner(System.in);

		// Read all numbers
		for (int i = 0; i < numbers.length; i++) {
			System.out.print("Enter a number:");

			// Conver String into integer
			numbers[i] = input.nextInt();
		}

		// Find the sum and average

		for (int i = 0; i < numbers.length; i++) {
			sum += numbers[i];

		}
		float average = sum / TOTAL_NUMBERS;

		// Find the number of values in the array greater than the average
		int maxAverage = 0;
		for (int i = 0; i < numbers.length; i++) {
			if (numbers[i] > average)
				maxAverage++;

		}

		// Prepare the result
		String output = "The array is";
		for (int i = 0; i < numbers.length; i++) {
			output += numbers[i] + " ";
		}

		output += "\nThe average is" + average;
		output += "\nThe number of values in the array greater than the average"
				+ "is" + maxAverage;

		// Display the result
		System.out.println(output);

	}
}

Casting is not a good idea for rounding numbers to int. If you use the code above (casting), it will not round your average to the nearest int but rather it will truncate the resulting number and drop the decimal places. A better and accurate way to do it is to use Math.round(float average) which will actually round your average to the nearest int.
Example:
let's say average = 5.9
by casting to int:
average = 5
by using Math.round:
average = 6

your code should be like this:

for(int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
float average = Math.round(sum / TOTAL_NUMBERS);


I recommended casting to JRabbit (if he wanted to use the integer option) because, in his code, his array is only storing integers. Where he is checking whether it's greater than average he has simply "if (numbers > average)," so if he used Math.round his count will be off because he doesn't have "if (numbers >= average)." If the average was 5.9 and you rounded, his code wouldn't count 6 as above average, which it is. So using his code, the casted version is actually what he would need. I suppose I should have recommended the floor function but that is a different story.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.