| | |
Having all kinds of problems
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 4
Reputation:
Solved Threads: 0
Could someone please help I am getting an illegal start of expression
Java Syntax (Toggle Plain Text)
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); } }
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.
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.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Mar 2009
Posts: 4
Reputation:
Solved Threads: 0
This is line 36
•
•
•
•
Could someone please help I am getting an illegal start of expression
Java Syntax (Toggle Plain Text)
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.
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.
Last edited by darkagn; Mar 23rd, 2009 at 4:01 am.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. 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:
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:
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
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:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Problems with new computer parts (Motherboards, CPUs and RAM)
- tooooo many problems! (Windows NT / 2000 / XP)
- System freezes and registry problems (Windows NT / 2000 / XP)
- CD burner Problems (Storage)
- Adding RAM to HP Pavillion 8776C - odd problems? (Motherboards, CPUs and RAM)
- My HJT and problems, please help. (Viruses, Spyware and other Nasties)
- WinXP and IE problems. Please Read. (Web Browsers)
- Overclocking 2600+.. help? (Motherboards, CPUs and RAM)
- Win Xp Pro Crashing (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: java code to connect printer
- Next Thread: facing problem in database connectivity in java to mysql
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows





