hi guys i'm trying to get the sum, average, and the number of values in the array greater than the average... i am new to this and keep getting errors! helllpp..

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 average = 0;
	for (int i = 0; i < numbers.length; i++) {
		if (numbers[i] > average) count++;
		
	}
	
	//Prepare the result
	String output = "The array is";
	for (int i = 0; i < numbers.length; i ++) {
		output += numbers[i] + " ";
	}
	
	output += "n\The average is" + average;
	output += "n\The number of values in the array greater than the average" + "is" + count;
	
	//Display the result
	System.out.println(output);
	
	}
}

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

the error line is under // Find the number of values in the array greater than the average

You forgot the '{' after the for statement on the block before this point.

Maybe not. The loop in lines 22/23 is a single statement loop. The calculation of the average on line 25 should be outside the loop, so the error is the superfluous } on line 18.

thanks masijade that helped alot. attention to detail right? unfortunately now i have another error

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 for line int average = sum / TOTAL_NUMBERS;:

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

Wrong fix (see previous post). By putting in the extra { you have (1) recalculated the average every time you add one number (10 times), rather than waiting to the end, and (2) left the declaration of average inside the unnecessary {} brackets, so it no longer exists when you try to use it in the next loop.
The error in line 25 is because you calculate a floating point value using sum, the assign that to an integer, thus losing any decimal places. In fact, since the raw data is int, sum can also be int, but average should probably be float.

//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;"

thanks i've made some changes what do you think?

//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++;
		
	}

now its saying

AverageArray.java:32: cannot find symbol
symbol : variable average
location: class AverageArray
if (numbers > average) maxAverage++;
^
AverageArray.java:42: cannot find symbol

Around this

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

either move the '}' above the average calculation (which is better IMHO) or remove both the '{' and the '}'.

I answered this in my previous post (and in my post on your duplicate thread).

masijade hi you said something about { not being there after the for statement ... i double checked and i dont see where its missing maybe i read your question wrong?

Jamescherrill you mentioned to take out } on line 18 which i did and put 25 outside the loop but now its giving me a "reached the end of file while parsing" error

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

AverageArray.java:50: reached end of file while parsing
}
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP

I give up. You make a change, then later claim that you don't where the change is to be made, then you make another change, that is the counter-point to the first change, with expected results, and wonder why it goes wrong.

All I can say now is, look at your code and balance your braces. for every '{' there should be a '}'. And indent your code properly. As it is now it is very hard to be read clearly.

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.