``````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);

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
``````

## 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

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);

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);``````

@jocamps
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, learning, and sharing knowledge.