Hi guys i'm new to programming and i'm supposed to do a sum then average in which the output requests for four numbers from user then takes those #'s add them up and get the average then do a number of values in the array greater than the average is... I keep getting stuck on the formula portions and im pretty sure there will be more errors after that but right now i'm trying to get past these to errors. please help! Thanks!

``````import java.util.Scanner;

public class AverageArray {
public static void main(String[] args) {
final int TOTAL_NUMBERS = 4;
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++)
float sum += numbers[i];

int average = float sum / TOTAL_NUMBERS;

}

//Find the average of the largest number
int count = 0
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == max) 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);

}
}
``````

errors

``````AverageArray.java:23: '.class' expected
float sum += numbers[i];
^
AverageArray.java:23: not a statement
float sum += numbers[i];
^
``````

## All 6 Replies

Probably an unmatched bracket? Re-post with code=java tags and properly indented

Probably an unmatched bracket? Re-post with code-java tags and properly indented

Mr. Cherrill I'm new to this daniweb where would I go about reposting with code-java tags?? I see the option for (code) on this message box I'll try that for now but thanks for the resonse.

You click the code button. Doing so produces the two tags that are enclosed in [][]. Before the first ']', put =Java. So it will say . Put your code inbetween so [code=Java]YOUR CODE HERE[othertag][code=Java]. Put your code inbetween so YOUR CODE HERE[othertag][code=Java]YOUR CODE HERE[othertag]

``````import java.util.Scanner;

public class AverageArray {
public static void main(String[] args) {
final int TOTAL_NUMBERS = 4;
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++)
float sum += numbers[i];

int average = float sum / TOTAL_NUMBERS;

}

//Find the average of the largest number
int count = 0
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == max) 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:23: '.class' expected
float sum += numbers;
^
AverageArray.java:23: not a statement
float sum += numbers;

``float sum += numbers[i];``

Here you are creating a new (uninitialsed) variable, then trying to add 1 to it. You probably didn't mean to create a new variable, so all you needed was

``sum += numbers[i];``

thanks that helped but now i came up with a different error... wow this site is great!

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.