Tom
Tom

Reputation: 19

java error ".class expected"

I am trying to call a method which calculate the average value in java. But when I compile it always output '.class' expected when it came to the line: System.out.println("Average: " + Average(double Value[]));

Here's my code:

public class q2
{
    public static void main(String[] args) throws IOException
    { 
        new q2().InputValue();
    }

public void InputValue() throws IOException
{  
    BufferedReader br = new BufferedReader(
                      new InputStreamReader(System.in)); 
    double[] Value = new double[10];
    for (int i = 0; i < 10; i++)
    {
        System.out.println("Please enter a value: ");
        Value[i] = Double.parseDouble(br.readLine());
    }
    System.out.println("Average: " + Average(double Value[]));
}

public double Average(double Value[])
{  
    double average = 0;
    for (int n = 0; n < 10; n++)
    {
        average = average + Value[n];
    }
    average = average / 10;
    return average;
}
}

Thanks

Upvotes: 0

Views: 22993

Answers (2)

Kajetan Abt
Kajetan Abt

Reputation: 1545

Suggestion: Use the List Interface with an ArrayList whenever you need an Array. It saves you from making stupid mistakes.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499860

This is the bit that's failing:

 "Average: " + Average(double Value[])

The double Value[] bit should be an argument for the method, e.g.

"Average: " + Average(Value)

I would strongly recommend that you start following normal Java naming conventions, e.g. naming classes with PascalCase, methods and variables with camelCase. Also, given that your Value variable actually holds multiple values, I'd pluralize it to values. You'd be amazed at how much easier code is to read when the names are chosen well :)

Upvotes: 7

Related Questions