943,824 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 535
  • Java RSS
Mar 23rd, 2009
0

Having all kinds of problems

Expand Post »
Could someone please help I am getting an illegal start of expression
Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class 9a
  4. {
  5. // The main method
  6. public static void main( String args[] )
  7. {
  8. Scanner input = new Scanner(System.in);
  9.  
  10. // declares and initializes the variables used to store user input.
  11. int n1;
  12. double radius, area;
  13.  
  14. int counter = 0;
  15.  
  16. for(n1 = 1; n1 <= 5; n1++)
  17. {
  18. // Get and store user input in variable num
  19. System.out.printf("Please enter the radius of circle %d : ", n1);
  20. radius = input.nextDouble();
  21. if (radius < 0) {
  22. System.out.println("This is an error, a radius can not be negative.");
  23. }
  24. else
  25. {area=calcarea(radius);
  26. System.out.printf("The circle with radius %d has area %d", radius, area);
  27. }
  28.  
  29. }
  30.  
  31.  
  32. // end of main method
  33.  
  34. // end class
  35.  
  36. public static Double calcarea()
  37. {
  38.  
  39. area = (radius * 3.14)^2;
  40. return(area);
  41. }
  42. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tkjr4160 is offline Offline
4 posts
since Mar 2009
Mar 23rd, 2009
0

Re: Having all kinds of problems

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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 23rd, 2009
0

Re: Having all kinds of problems

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 }
Last edited by tkjr4160; Mar 23rd, 2009 at 2:33 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tkjr4160 is offline Offline
4 posts
since Mar 2009
Mar 23rd, 2009
0

Re: Having all kinds of problems

Which line is line 36 of your file?
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 23rd, 2009
0

Re: Having all kinds of problems

This is line 36
Click to Expand / Collapse  Quote originally posted by tkjr4160 ...
Could someone please help I am getting an illegal start of expression
Java Syntax (Toggle Plain Text)
  1.  
  2. public static double calcarea()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tkjr4160 is offline Offline
4 posts
since Mar 2009
Mar 23rd, 2009
0

Re: Having all kinds of problems

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.
Last edited by darkagn; Mar 23rd, 2009 at 4:01 am.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 23rd, 2009
0

Re: Having all kinds of problems

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:
Java Syntax (Toggle Plain Text)
  1. public static double calcarea(double radius)
  2. {
  3.  
  4. area = (radius * 3.14)^2; // idd, don't just copy paste this, like I did :)
  5. return(area);
  6. }

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)
  1. public static double calcarea(double radius)
  2. {
  3. [B]double[/B] area = (radius * 3.14)^2; // idd, don't just copy paste this, like I did :)
  4. return(area); // these brackets are not mandatory return area; is also correct
  5. }

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
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is online now Online
2,497 posts
since Jan 2007
Mar 24th, 2009
0

Re: Having all kinds of problems

thanks guy both you guys helped
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tkjr4160 is offline Offline
4 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: boolean help!
Next Thread in Java Forum Timeline: facing problem in database connectivity in java to mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC