• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Am i getting this error?  RSS feed

 
Ryan Gordon
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remove public double calculateDistance() my program compiles and runs fine but when it is in the program it gives this error. I need to have the method as my api for an assignment, just not sure exactly how to implement it properly.



 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't define a method within a method.

 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've put the calculateDistance() method inside your main(String...) method. You can't put one method inside another method.

And from the design point of view, you have way too much code inside that calculateDistance() method. You have code which reads x values and y values from the console; that has nothing to do with calculating distances. In fact only one line of your code has anything to do with calculating distances. So your method should only contain that one line of code, plus a return statement.

However... your posts here all seem to show you putting lines of code in random places and hoping they might work. Learning Java that way is going to be a very long and tedious process; for example all I can say about the code here is that you declared the method in the wrong place. Telling you how to fix it would require a lot of explaining things which you ought to have been taught by now. Sure, it's natural that your teacher might say something that you missed, or that went over your head, but I think you need to go back to your teacher and ask for help getting out of the mess you're in now.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't use the sqrt method for calculating Pythagoras: try this instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!