Hiya, im new to this site but it seems like a very useful comunity :)

Im new to java programming as well and i have to write a class file to deal with calculating the maximum, minimum, average(mean), population variance and sample variance from an array which is entered by the user which is delt with in the array application .

This is what i have done so far but when i go to compile it i get an error saying "illegal start of type" at this line (for double i = o; i < doubleArray.length; i++); and i really cant see why its an illegal start :(

im sure there are many other errors too :(

// ArrayCalculator Class file

// Author: 590028847

// 08/11/09

public class ArrayCalculator
{

   // A method for returning the maximum number in the array
   public double max;
    (for double i = o; i < doubleArray.length; i++);
   {
       while(array[i]>max)
       {
       max = array[i];
        }
    }
    return ("Maximum value:" + max);

    // A method for returning the minimum number in the array
    public double min;
    for(double i = o; i < doubleArray.length; i++)
    {
       while(array[i]<min)
       {
       min = array[i];
        }
    }
    return ("Minimum value:" + min);
    
    // A method to calculate the sum of the array
    public double sum = 0;
    for (double i = 0; i < doubleArray.length; i++){}
    
    // A method to return the mean of the array
    public double mean = sum / doubleArray.length;
    return ("Average (mean):" = mean);
    
    // A method to return the population variance
    double populationVariance = Math.pow((sum - mean),2) / doubleArray.length;
    return ("Population Variance:" = populationVariance);
    
    // A method to return the sample variance
    double sampleVariance = Math.pow((sum - mean),2) / ( doubleArray.length - 1);
    return ("Sample Variance:" = sampleVariance);
}

Then the application gets an error of illegal start of opperation at line 22

// ArrayCalculatorApp Application
// Program does not carry out any exception handling or type checking on input
// Generates an array of type double whose contents are specified by the user

// Author: 590028847

// 08/11/09

// Import classes used from java .io package
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ArrayCalculatorApp
{
   public static void main(String[] args) throws java.io.IOException{
       // Create BufferedReaderClass instance
       InputStreamReader input = new InputStreamReader(System.in);
       BufferedReader keyboardInput = new BufferedReader(input);
       
       // Declair and assign values to array
       System.out.println("Please enter values seperated by a comma then a space, e.g 1, 2, 3");
       public double[]doubleArray = {(keyboardInput.readLine()).doubleValue()};
       
       // To print details to screen
       System.out.println(max)
       System.out.println(min)
       System.out.println(mean)
       System.out.println(populationVariance)
       System.out.println(sampleVariance)
    }
}

help would be much apreciated to a newbie at programming and this site
Cheers
Matt

Recommended Answers

All 7 Replies

The letter 'o' is not a 0 Zero.

The letter 'o' is not a 0 Zero.

heya, thanks for the reply and spotting that,
i still get the illegal start of type error when i try to compile it though :( any ideas ?

Yes, you've declared a class ArrayCalculator, but no methods in it containing your lines of code... just lines of code.

Let me show you ONE method

// ArrayCalculator Class file

// Author: 590028847

// 08/11/09

public class ArrayCalculator
{
	///////////////////////////////////////////////////////////
	// A method for returning the maximum number in the array
	public static double max(double[] doubleArray)
	{
		double dblRetVal = doubleArray[0];

		for (int i = 0; i < doubleArray.length; i++)
		{
			if(dblRetVal < doubleArray[i])
			{
				dblRetVal = doubleArray[i];
			}
		}

		return dblRetVal;
	}
}

Here is a small tester that can show you how it can be called.

import java.io.*;

class ArrayCalculatorApp
{
	public static void main(String[] args) throws java.io.IOException
	{
		double[] doubleArray = {3,5,8,2};

		System.out.println("The MAX is : " + ArrayCalculator.max(doubleArray));
	}
}

Study the structure of the ArrayCalculatorClass and add your other methods.

Notice that the max() function JUST returns a double.

You have a semicolon after a for-loop:

(for double i = o; i < doubleArray.length; i++);

Removing that should clear up that error you mentioned.

Brownjohn, please take note that there are no methods/functions in the class (or at least not the right syntax).

Removing the semi-colon is necessary, but will only expose a multitude of other problems to include needing to change that particular "double" to an "int".

Matt547, are you allowed to use static methods?
If there has been no mention of static methods, can you assume it's OK to use them? ;)

...if so, the last few lines of your "main" will look like this:

System.out.println("The MAX is : " + ArrayCalculator.max(doubleArray));
System.out.println("The MIN is : " + ArrayCalculator.min(doubleArray));
System.out.println("The SUM is : " + ArrayCalculator.sum(doubleArray));
System.out.println("The MEAN (average) is : " + ArrayCalculator.mean(doubleArray));
System.out.println("The populationVariance is : " + ArrayCalculator.populationVariance(doubleArray));
System.out.println("The sampleVariance is : " + ArrayCalculator.sampleVariance(doubleArray));

Brownjohn, please take note that there are no methods/functions in the class (or at least not the right syntax).

Removing the semi-colon is necessary, but will only expose a multitude of other problems to include needing to change that particular "double" to an "int".

You're right, I just noticed other syntax errors, including a misplaced "(" at the beginning of the for loop.

My recommendation to the OP would be to go line-by-line and clean up all the syntax errors first.

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.