954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Having all kinds of problems

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);
	}
}
tkjr4160
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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 }

tkjr4160
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Which line is line 36 of your file?

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

This is line 36
Could someone please help I am getting an illegal start of expression

public static double calcarea()
tkjr4160
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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)
{ 
	<strong>double</strong> 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

stultuske
Posting Sensei
3,110 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 432
 

thanks guy both you guys helped

tkjr4160
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You