Having all kinds of problems

Thread Solved

Join Date: Mar 2009
Posts: 4
Reputation: tkjr4160 is an unknown quantity at this point 
Solved Threads: 0
tkjr4160 tkjr4160 is offline Offline
Newbie Poster

Having all kinds of problems

 
0
  #1
Mar 23rd, 2009
Could someone please help I am getting an illegal start of expression
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 790
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Having all kinds of problems

 
0
  #2
Mar 23rd, 2009
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.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: tkjr4160 is an unknown quantity at this point 
Solved Threads: 0
tkjr4160 tkjr4160 is offline Offline
Newbie Poster

Re: Having all kinds of problems

 
0
  #3
Mar 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 790
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Having all kinds of problems

 
0
  #4
Mar 23rd, 2009
Which line is line 36 of your file?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: tkjr4160 is an unknown quantity at this point 
Solved Threads: 0
tkjr4160 tkjr4160 is offline Offline
Newbie Poster

Re: Having all kinds of problems

 
0
  #5
Mar 23rd, 2009
This is line 36
Originally Posted by tkjr4160 View Post
Could someone please help I am getting an illegal start of expression
  1.  
  2. public static double calcarea()
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 790
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Having all kinds of problems

 
0
  #6
Mar 23rd, 2009
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.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Having all kinds of problems

 
0
  #7
Mar 23rd, 2009
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:
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: tkjr4160 is an unknown quantity at this point 
Solved Threads: 0
tkjr4160 tkjr4160 is offline Offline
Newbie Poster

Re: Having all kinds of problems

 
0
  #8
Mar 24th, 2009
thanks guy both you guys helped
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC