Could someone please help I am getting an illegal start of expression

import java.util.Scanner;

public class 9a
{
	// The main method
	public static void main( String args[] )
	{
		Scanner input = new Scanner(System.in);

		// declares and initializes the variables used to store user input.
		int n1;
		double radius, area;

		int counter = 0;
		
		for(n1 = 1; n1 <= 5; n1++)
			{
		// Get and store user input in variable num
		System.out.printf("Please enter the radius of circle %d : ", n1);
		radius = input.nextDouble();
		if (radius < 0) {
		System.out.println("This is an error, a radius can not be negative.");
		}
		else 
		{area=calcarea(radius);
		System.out.printf("The circle with radius %d has area %d", radius, area);		
		}			
						
	}


	// end of main method

// end class

	public static Double calcarea()
{
 
	area = (radius * 3.14)^2;
	return(area);
	}
}

Recommended Answers

All 7 Replies

area = (radius * 3.14)^2;

Two things: first, the area of a circle is PI * r^2 not (r * PI)^2. Secondly, the java compiler doesn't recognise the ^. You need to use the Math.pow method to raise something to a power, but in this case it is easier to just multiply PI by radius by radius.

ok i fixed the math error here are my errors when i compile

36 illegal start of expression
I can't figure this out
I assume these below are because of 36
42 ;
42 }

Which line is line 36 of your file?

This is line 36

Could someone please help I am getting an illegal start of expression

public static double calcarea()

Ah, ok, you need to move your calcarea method inside the class.

EDIT: No wait, it is inside the class. This exception is usually raised when you miss closing a bracket or brace or miss a semi-colon. Double check your code to make sure that hasn't happened. Also "Double" should be "double" in the calcarea signature.

just a few remarks:

you're calling the calcarea - method with a parameter, but you have no parameter calcarea that takes parameters
it should take the parameter 'double radius'
like this:

public static double calcarea(double radius)
{
 
	area = (radius * 3.14)^2; // idd, don't just copy paste this, like I did :)
	return(area);
	}

also: count your closing brackets: your calcarea method is in your main method, and you don't have a closing bracket for the class

your method calcarea also does not know the local variable area, so a next improvement to your method above:

public static double calcarea(double radius)
{ 
	[B]double[/B] area = (radius * 3.14)^2; // idd, don't just copy paste this, like I did :)
	return(area); // these brackets are not mandatory return area; is also correct
	}

I don't know if there are any problems left, I haven't tested the app, but this should help you at least a bit further
d

thanks guy both you guys helped

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.